Bixar
Bixar

Reputation: 111

Assistance with javascript palindrome

I am trying to solve a palindrome using a for loop in JavaScript (my code is below).

I cannot figure out what I am doing wrong. If someone can please correct and explain, it will be much appreciated. I am fairly new to coding.

var word = window.prompt("Enter a word");
function palindrome(a) {
  var reversed = '';
  for (i = 0; i <= a.length; i++) {
    reversed = reversed + a[a.length - 1 - i];
  }
  if (a == reversed) {
    return "true";
  } else {
    return "false";
  }
}
document.write(palindrome(word));

Upvotes: 0

Views: 81

Answers (3)

user2182349
user2182349

Reputation: 9782

This code compares the first character to the last character, then advances to compare the second character to the next to last character until it runs out of characters.

As soon as it finds an inequality, it returns false because there is no reason to continue comparing.

let word = window.prompt("Enter a word");
const palindrome = a => {
  let last = a.length - 1;
  // loop, comparing the values until you find something that doesn't match
  for (let first = 0; first <= last; first++, last--) {
    if (a[first] !== a[last]) {
      return "false";
    }
  }
  // everything matched
  return "true";
}
document.getElementById("output").textContent = palindrome(word);
<output id="output"></output>

Upvotes: 0

Erick Cruz
Erick Cruz

Reputation: 66

your loop:

for (i = 0; i <= a.length; i++) {
    reversed = reversed + a[a.length - 1 - i];
  }

you just need remove -1 and start the loop with 1 because when you reached the end of the iteration you will have the length of the word -1 and this will try to access a negative position.

after changing:

 for (let i = 1; i <= a.length; i++) {
       // you can use reversed += a[a.length - i] instead of;
       reversed = reversed + a[a.length - i];   
     }

You can also reverse a string using the reverse method like this:

 reversed = a.split('').reverse().join(''); 

Finally if you want to validata sentences you need to remove blank spaces and convert it in lower or upper case(usually converted in lowercase) because compare is case sensitive ("Party trap" != "part ytraP").

Upvotes: 0

Derek Wang
Derek Wang

Reputation: 10204

On for loop inside palindrome, you have looped from 0 ~ a.length and the item on length index in a string is undefined so reversed will always be undefined.

You need to loop from 0 ~ a.length - 1 as follows.

var word = window.prompt("Enter a word");
function palindrome(a) {
  var reversed = '';
  for (i = 0; i < a.length; i++) {
    reversed = reversed + a[a.length - 1 - i];
  }
  console.log(reversed);
  if (a == reversed) {
    return "true";
  } else {
    return "false";
  }
}
document.write(palindrome(word));

You can reverse string simply as follows.

var word = window.prompt("Enter a word");
function palindrome(a) {
  const reversed = a.split('').reverse().join('');
  if (a == reversed) {
    return "true";
  } else {
    return "false";
  }
}
document.write(palindrome(word));

Upvotes: 1

Related Questions