Problem with Palindrome code [SPOILERS (maybe)]

Problem with Palindrome code [SPOILERS (maybe)]
0.0 0

#1

Hey, so I’m currently working on the palindrome exercise, this is obviously no the full code, I’m just testing how i can check every letter first.

This is what I have so far:

function palindrome(str) {
  
  var abc = "abcdefghijklmnopqrstuvwxyz";
  var current = 0;
  var palin = [];
  
  for (var i = str[current]; current < str.length; current++) {
    var j = i.toLowerCase();
    if (abc.includes(j)) {
      palin.push(j);
    }
  }
  return palin;
}


palindrome("abc");

Why does my array “palin” return [“a”,“a”,“a”]? I’m sure current it’s adding up every iteration, so why aren’t i getting [“a”,“b”,“c”]?

Thank you in advance, guys.


#2

You’re using the loop incorrectly.

for ( var i = 0; i < str.length; i++ ) { 

for starters.
You’ve declared current as being zero. It will not change.


#3
function palindrome(str) {
  
  var abc = "abcdefghijklmnopqrstuvwxyz";
  var current = 0;
  var palin = [];
  
  while(current <= str.length-1) {
     if(abc.includes(str[current].toLowerCase())) {
        palin.push(str[current].toLowerCase());
     }
     ++current;
  }
 return palin;
}

You can use while loop too.


#4

Ah, thank you!, this is easier, when I think of iterations my head instantly things of for loops


#5

Yes me too :slight_smile: … just for loops are the most common usage of loops and you can write same things using different loops, just we reach for for loops by default :slight_smile:


#6

I got another problem.
This is almost working, my problem is kind of weird.
Here’s my code:

function palindrome(str) {
  
  var abc = "abcdefghijklmnopqrstuvwxyz";
  var current = 0;
  var palin = [];
  
  while (current <= str.length-1) {
    if (abc.includes(str[current].toLowerCase())) {
      palin.push(str[current].toLowerCase());
    }
    current ++;
  }
  var newStr = palin.join("");
  var palinRev = palin.reverse();
  var strRev = palinRev.join("");
  return newStr === strRev;
}


palindrome("1 eye for of 1 eye.");

This works for almost all tests, except for the one i used in the function, “1 eye for of 1 eye”.
It says it should return false, but my code returns true, I’m not sure what’s happening, my code removes everything that’s not a letter then checks if it’s the same backwards.


#7

The problem is you are not allowing numbers (0-9) to be counted as a valid characters in the palindrome check.
Below is from the challenge instructions:

Note
You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols)
 and turn everything lower case in order to check for palindromes.

Alphanumeric characters are valid.


#8

Ah! Thank you, I thought only letters were allowed, I’ll go fix it in a sec


#9

Briefly looking at your code, it looks like all you’d need to fix in what you posted was your for loop (and also allowing numbers as you mention before). A for loop works such that:
for(a; b; c) “a” is done only once at the beginning of the loop, “b” is checked each iteration of the loop before the loop is executed, and “c” is done each time at the end of the loop. Thus it would be like:

var i = 0;
while (true) {
  if(i < 5){
    // code in loop here
    i++;
  } else {break;}
}

(where break exits the infinite loop) to match:

for (var i = 0; i < 5; i++){
  // code in loop here
}

Essentially, your problem in the for loop is that i never got updated after it was initialized with var i = str[current]. Hope that helps with understanding the for loop and the bug you were getting!