Created
February 28, 2013 22:07
-
-
Save jesseditson/5060543 to your computer and use it in GitHub Desktop.
A small node script for detecting if a word is a palindrome
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
#!/usr/bin/env node | |
var word = process.argv.slice(2)[0] | |
if(!word) return console.error("Please call this script with a word, e.g.: \n node pal.js someword") | |
// split the word into letters | |
var letters = word.split('') | |
var i=0 | |
var len = letters.length | |
var max = len-1 | |
// iterate over each letter | |
for(i;i<len;i++){ | |
// if we've gotten to the middle of the word, they've all passed, and this is a palendrome. | |
if(i >= len/2) return console.log("That word is a palindrome!") | |
// if any letter does not match the same letter from the end of the word, this is not a palendrome | |
if(letters[i] != letters[max-i]) return console.log("That word is not a palindrome.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment