I need to write the function "isPalindrome()" so that it loops through the given word and tests whether the word reads the same forward and backward. A palindrome is a word that is the same forwards and backwards. The word I want to use is SAGAS. The language I am using to write the code is JavaScript and I would like it to be an if statement withing the while loop that I have below. Here is what I have so far:
function isPalindrome(SAGAS)
{
var left = 0, right = phrase.length - 1;
var palindrome = true;
i came up with a non recursive solve.
but first some help
how would one go about comparing the word sagas?
compare the first to the last letter
then compare the second and the fourth letter,and so on
the middle letter is the same as itself ofcourse no need to test that.
here is my solve,but please try and come up with your own solution,it might be even better!
1 2 3 4 5 6 7 8
for(int i =0;i<str.length();i++){
if(str[i]==str[stringLength-i-1]){
isPalindrome=true;
}else{
isPalindrome=false;
break;
}
}