Created
April 17, 2020 20:29
-
-
Save Nicknyr/316b38f1288bfd710a9cb74f92a45c34 to your computer and use it in GitHub Desktop.
CodeSignal - Character Parity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a character, check if it represents an odd digit, an even digit or not a digit at all. | |
Example | |
For symbol = '5', the output should be | |
characterParity(symbol) = "odd"; | |
For symbol = '8', the output should be | |
characterParity(symbol) = "even"; | |
For symbol = 'q', the output should be | |
characterParity(symbol) = "not a digit". | |
*/ | |
function characterParity(symbol) { | |
// Turn the string into a number | |
let num = parseInt(symbol, 10); | |
// If num is not a number it's a letter | |
if(isNaN(num)) { | |
return "not a digit"; | |
} | |
else { | |
if(num % 2 === 0) { | |
return "even"; | |
} | |
else { | |
return "odd"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment