Basic Algorithm Scripting: Confirm the Ending - what are JS substring methods?

Tell us what’s happening:
The instructions to the challenge state, “for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.”
What are Javascript substring methods?

Your code so far


function confirmEnding(str, target) {
return str;
}

confirmEnding("Bastian", "n");

Your browser information:

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

Challenge: Confirm the Ending

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

1 Like

As a developer, one of the most useful references I have is the MDN website. If I do a google search for “mdn substring” I get this link. Also substr and slice are useful.

Let us know if you’re still having difficulty.

1 Like

Thanks, I use MDN a lot to look up stuff and I had already found these links. I wasn’t sure if that’s what they were referring to in the instructions (substring, substr, slice). I wasn’t sure if these were all the substring methods or only a few of many more. In any case I haven’t yet figured out how to use them to solve the challenge so I will keep trying.

1 Like

Yeah, the phrase “one of the JavaScript substring methods” seems a bit vague, but this is what it would imply to me.

1 Like

Here is my code below but it does not work. Any ideas why not?

function confirmEnding(str, target) {
     if (/target$/.test(str))  {
         return true;
     } else {
     return false;
   }
}

confirmEnding("Bastian", "n");

you cant use variables or parameters in regex like that you have to use it with RegExp()

let reg = RegExp(target + '$')
     if (reg.test(str))  {
         return true;
     } else {
     return false;
   }

Thanks. I am not familiar with RegExp() so I used the substring method instead.

1 Like