Simple node.js palindrome checker for the command line.
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.question('Please enter potential palindrome: ', checkForPalindrome); | |
function checkForPalindrome(str) | |
{ | |
var resultText = ""; | |
// Only allow upper and lower case letters | |
if (/^[a-zA-Z]+$/.test(str)) | |
{ | |
var result = true; | |
resultText = "\"" + str + "\"" + " is a palindrome!"; | |
var strLowerCase = str.toLowerCase(); | |
for (var i = 0; i < str.length/2; i++) | |
{ | |
var currentLetterLeft = strLowerCase[i]; | |
// Remember to substract 1 from length because of zero-indexing strings | |
var currentLetterRight = strLowerCase[str.length-1-i]; | |
if (currentLetterLeft != currentLetterRight) | |
{ | |
result = false; | |
resultText = "\"" + str + "\"" + " is NOT a palindrome!"; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
resultText = "Only upper and lower case letters are allowed!"; | |
} | |
console.log(resultText); | |
rl.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment