Character Parity
### Given a character, check if it represents an odd digit, an even digit or not a digit at all —
function characterParity(symbol) {
const result = parseInt(symbol);
return isNaN(result) ? 'not a digit' : result % 2 === 0 'even' : 'odd'
}
console.log(characterParity('5')) odd
console.log(characterParity('8')) even
console.log(characterParity('q')) not a digit
⚙️ Implementation Details
Parameters
symbol (String): The character to be evaluated (e.g., ‘5’, ‘8’, ‘q’).
Step-by-Step Logic
Convert the Character: Use parseInt(symbol, 10) to convert the input character into an integer.
Validate the Input: Use isNaN(result) to check whether the converted value is a valid number.
Not a Digit: If the result is NaN, return ‘not a digit’ because the input cannot be converted into a number.
Check Even or Odd: Use the modulo operator (result % 2 === 0) to determine whether the number is divisible by 2.
Even Numbers: If the remainder is 0, return ‘even’.
Odd Numbers: If the remainder is not 0, return ‘odd’.
Return the Result: Return the appropriate string based on the input character.
Examples
characterParity(‘5’) // → ‘odd’ characterParity(‘8’) // → ‘even’ characterParity(‘q’) // → ‘not a digit’ —
comments powered by Disqus