For loop method in palindrome checker exam *Solved*

Tell us what’s happening:
I don’t understand the problem with the code. Am I misunderstanding the project or the code itself has a fault I am not seeing.

  **Your code so far**

function palindrome(str) {
let palindromes = [str.slice(str)]
for( let i = palindromes.length- 1; i > -1; i--){
  console.log(palindromes.splice(i))
  console.log([str.slice(str)])
  return palindromes.splice(i) === [str.slice(str)];
}
}



palindrome("eye");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36

Challenge: Palindrome Checker

Link to the challenge:

.splice() changes the array as you iterate over it, which makes a mess of your indexing. You should not modify an array as you loop over it, in general.

2 Likes

I mean, what is the code even supposed to do?
Neither slice nor splice achieve anything. The for-loop runs only once. And you are comparing two arrays, which JS propably does via memory adress → so that will always fail, unless it’s literally the exact same variable.

You need to rethink your approach. You are given a string and are supposed to check if reading it backwards is the same as forwards. How would you do this in real-life?

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.