Bonfire: Confirm the Ending

On a roll! Just did two FreeCodeCamp Bonfire Algorithms back-to-back. This one took me about 20 minutes. Use .substr() method.

The Challenge:

Check if a string (first argument) ends with the given target string (second argument).

The Solution:

function end(str, target) {
  var lastLetter = str.substr(-(target.length), target.length);
  if (target === lastLetter) {
    return true;
  } else {
    return false;
end("He has to give me a new name", "name")
end("Bastian", "n", "");

Use console.log(lastLetter); after defining the variable lastLetter to see what is printing. It really helps to see what is going wrong with your code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s