Skip to content

Instantly share code, notes, and snippets.

@pReya
Last active October 22, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pReya/6203fe96fe3067c6f92f2a011bba1002 to your computer and use it in GitHub Desktop.
Save pReya/6203fe96fe3067c6f92f2a011bba1002 to your computer and use it in GitHub Desktop.
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