Palindrome Certification Challenge Bug?

Tell us what’s happening:
//pay attention to the output.

when I run this code, and it reaches the “panama” palindrome that is supposed to return true, it seems to read an extra m at the end of the first and second indices.

i tried to comment this code as best I can.

Your code so far


/*
code puts strings into two indecies; one forward and the other backwards. if the values match, the code will return true. 
*/

let letterValIndex = [];
let letterValIndex2 = [];


let result;

function clearIndex(){
 letterValIndex = [];
 letterValIndex2 =  [];
}
// compares indexes after the have been processed by valAsign()
function compareIndex(a,b){
  
  if (JSON.stringify(a) == JSON.stringify(b)){
    result = true;
    console.log("true");
    return;
  }
else if(JSON.stringify(a) != JSON.stringify(b)){
   result = false;
   console.log("false");
   return;
}
    } 
//Asigns both capital and lowercase versions of a letter as one value
function valAsign(inputStr) {
let tested;

for (let char in inputStr){

switch(inputStr[char]){
 case ("a" || "A"): tested = 1;
letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("b" || "B"): tested = 2;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("c" || "C"): tested = 3;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("d" || "D"): tested = 4;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("e" || "E"): tested = 5;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("f" || "F"): tested = 6;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("g" || "G"): tested = 7;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue; 
 case ("h" || "H"): tested = 8;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("i" || "I"): tested = 9;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 
 case ("j" || "J"): tested = 10;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("k" || "K"): tested = 11;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("l" || "L"): tested = 12;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("m" || "M"): tested = 13;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("n" || "N"): tested = 14;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("o" || "O"): tested = 15;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("p" || "P"): tested = 16;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("q" || "Q"): tested = 17;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("r" || "R"): tested = 18;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("s" || "S"): tested = 19;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("t" || "T"): tested = 20;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("u" || "U"): tested = 21;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("v" || "V"): tested = 22;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("w" || "W"): tested = 23;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("x" || "X"): tested = 24;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("y" || "Y"): tested = 25;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 case ("z" || "Z"): tested = 26;
 letterValIndex.push(tested);
 letterValIndex2.unshift(tested);
 continue;
 
 
 }
//punctuation saftey net
 if (char == " "){
   tested == undefined;
 }
 else if(char == ","){
   tested == undefined;
 }
 else if("."){
   tested == undefined;
 }
 else if(tested == undefined){
   continue;
 }
 else{
   letterValIndex.push(tested);
   letterValIndex2.unshift(tested);

 }
}
console.log(letterValIndex);
 console.log(letterValIndex2);
}



//mainFunction
function palindrome(str){

valAsign(str);

compareIndex(letterValIndex,letterValIndex2);

clearIndex();


return result;

}






palindrome("eye");

Your browser information:

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

Challenge: Palindrome Checker

Link to the challenge:

Your code currently is ignoring uppercase letters.
The way you’re checking for multiple values in case is not how switch works. You should use fall-through:

case 'a':
case 'A':
  tested = 1;
  ...
1 Like

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like