1

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?

  • 1
    you can not compare two arrays with the same content, but with different object references. you need to compare the item. – Nina Scholz Mar 18 '18 at 08:20
  • You should read this question https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript and its answers – Ray Toal Mar 18 '18 at 08:21
  • Got it, I didn't know arrays couldn't be compared like primitive data types. – Victor Evangelista Mar 18 '18 at 08:24
  • A little help : `a = [1,2,3]; b = [1,2,3]; console.log(a === a); /* true */ console.log(a === b); /* false */` :-) –  Mar 18 '18 at 08:47

2 Answers2

0

You can not compare two arrays with the same content, but with different object references. You need to compare the item by using a counter for the characters who are equal end iterate from the end of the string.

const confirmEnding = (str, target) => {
  var i = 0;
  while (i < target.length && str[str.length - 1 - i] === target[target.length - 1 - i]) {
      i++;
  }
  return i === target.length;
}

console.log(confirmEnding("Congratulation", "on"));
console.log(confirmEnding("Congratulation", "off"));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

You can change your logic to make it simple. Just get the lastIndexOf the target string from the str so that you can take substring of the last word and compare it with the target:

const confirmEnding = (str, target) => {
  var indexOfTarget = str.lastIndexOf(target);
  var lastStr = str.substr(indexOfTarget, str.length - 1);
  if(lastStr === target){
    return true;
  }
  return false;
};
//match
console.log(confirmEnding("Congratulation", "on"));
//match
console.log(confirmEnding("Congratulation", "tion"));
//no match
console.log(confirmEnding("Congratulation", "ons"));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • You don't answer the question : "Why does the boolean return false?". –  Mar 18 '18 at 08:37
  • @leaf I understood your comment but I preferred to suggest a better answer despite of giving the reason for the answer which is not more appropriate to achieve what OP wants to achieve. – Ankit Agarwal Mar 18 '18 at 08:46
  • Then [your logic](https://stackoverflow.com/questions/49345837/confirming-a-matching-ending-boolean-issue-javascript/49345879?noredirect=1#comment85691538_49345871) is not coherent :-P –  Mar 18 '18 at 08:50