Last active
October 22, 2017 21:10
-
-
Save pReya/6203fe96fe3067c6f92f2a011bba1002 to your computer and use it in GitHub Desktop.
Simple node.js palindrome checker for the command line.
This file contains 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
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