Confirm the Ending (Unable to match)

Hi,

I attempted to match the ending for the first case (bastian,n) but somehow it doesn’t seem to return to true.

I also realised that I am supposed to use a substring method, but I am still curious as to why it wouldn’t return to true in my code (at least for the first case).

function confirmEnding(str, target) {
// split str into elements array --> newStr
let newStr = str.split("");
//console.log(newStr);
// split target into elements array --> newTarget
let newTarget = target.split("");
//console.log(newTarget);

//  -- slice returns the portion cut out as a new array;
let lengthToSlice = newStr.length - newTarget.length; //index of where to start slicing, including the point of starting
console.log(lengthToSlice);

let stringToCheck = newStr.slice(lengthToSlice);

    //check if newStr array == newTarget array
console.log(stringToCheck);
console.log(newTarget);

   if (stringToCheck == newTarget){
    return true;
   } else {
     return false;
   }

}

confirmEnding("Bastian", "n");

Thanks!

Hi cornstar,

Your if statement is comparing 2 arrays and unfortunately you can not compare arrays like this (it’s complicated, but there’s some good info on Stack Overflow, such as here

Changing your code to the below is one way to get it to work as it will then compare the strings in the arrays:

if (stringToCheck[0] == newTarget[0]) {
  return true;
} else {
  return false;
}   

Thanks for helping me with this problem! :slight_smile: