I wonder how to write palindrome in javascript, where I input different words and program shows if word is palindrome or not. For example word noon is palindrome, while bad
function palindrome(str){
for (var i = 0; i <= str.length; i++){
if (str[i] !== str[str.length - 1 - i]) {
return "The string is not a palindrome";
}
}
return "The string IS a palindrome"
}
palindrome("abcdcba"); //"The string IS a palindrome"
palindrome("abcdcb"); //"The string is not a palindrome";
If you console.log this line: console.log(str[i] + " and " + str[str.length - 1 - i]), before the if statement, you'll see what (str[str.length - 1 - i]) is. I think this is the most confusing part but you'll get it easily when you check it out on your console.