Confirm the Ending (I don't really need help, just wanted to say that the FCC solution to this is pretty cool)

Tell us what’s happening:
So I solved this using regex, but the “native” solutions is this:

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  
  return str.slice(str.length - target.length) === target;
}

confirmEnding("He has to give me a new name", "name");

Just found it to be really clever. I’m new btw so my mind is still blown by these kinds of codes.

Your code so far


function confirmEnding(str, target) {
  let regex = target;
  let testString = new RegExp(regex + "$", "i")
  let finalTest = testString.test(str);



  return finalTest;
}

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/69.0.3497.100 Safari/537.36.

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

Even simpler:

function confirmEnding(str, target) {
  return str.slice(-target.length) === target
}

Array.prototype.slice

A negative index can be used, indicating an offset from the end of the sequence.

Or using ES6 Arrow functions:

const confirmEnding = (str, target) => str.slice(-target.length) === target

function confirmEnding(str, target) {
return str.slice(-target.length) === target
}
why am I even trying to study coding xD After how long before you gain mastery like this?

Hang out in the Gitter channels, we happened to discuss this problem just today!

which channel? most of the time I’m here, no one really says anything :expressionless:

The Help and HelpJavascript channels tend to be the most active, but it’s sporadic. There’s usually people in there during the day (US) but not always talking.

1 Like