SPOILER! Confirm the Ending

My code passes the test, but I’m wondering if there is a difference between using .slice() which is what FCC answer uses versus .substring. Should I stick to using .substring or is there an advantage of using .slice()?

Your code so far


function confirmEnding(str, target) {
  let end = str.substring(str.length-target.length);

  if (target===end){
    return true;
  }
  else {
    return false;
  }
}

console.log(confirmEnding("Bastian", "n"));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending

1 Like

You got me wondering this myself…I found a stackoverflow answer that breaks down how they each work pretty well: https://stackoverflow.com/a/31910656
Actually all the answers there are pretty good haha.

1 Like

Thank you for the resource! It answered the doubts I had. Pretty funny how he just states that slice === substring :joy:

1 Like

Right?? I swear half the time, explanations of JS concepts just make them more confusing then they were before hahaha. I guess the real understanding comes from just messing around with them, trying different things and noting the results. :nerd_face:

1 Like

Yup! Understanding comes with experience, I guess :blush:

1 Like