I'm attempting to create a function that returns if the ending of a string is the same as a given variable without using .endsWith().
I'm not sure why this isn't working. Chaining .join("") and comparing the two values as strings works, but not as arrays.
const confirmEnding = (str, target) => {
// split string into array, splice end of array based on target length
console.log(str.split("").splice(str.length - target.length, target.length));
// split target into array
console.log(target.split(""));
// compare two arrays
return str.split("").splice(str.length - target.length, target.length) === target.split("");
console.log(confirmEnding("Congratulation", "on"));
OUTPUT
[ 'o', 'n' ]
[ 'o', 'n' ]
false
Clearly, the arrays are exactly the same. Why does the boolean return false?