Confirm the Ending - Alternative solution


function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  return !!str.match(target + '$')
  //return str;
}

confirmEnding("Bastian", "n");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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

Hi,

You have to substract the last characters of your string with str.substr() method. Here you can see how it works: https://www.w3schools.com/jsref/jsref_substr.asp

and the length of the target should help you to match the last characters of the string.

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

Thank you, substr is good enough.

1 Like
function confirmEnding(str, target) {
  let splitStr=str.split("");
  let splitTarget=target.split("");

  for(let i=0;i<splitTarget.length;i++){
    if(splitTarget[i] != splitStr[splitStr.length-splitTarget.length+i])
      return false;
  }
  return true;


}