Skip to content

Instantly share code, notes, and snippets.

@AndriiBozh
Created January 11, 2018 22:10
Show Gist options
  • Save AndriiBozh/e98687c4577044c5526cd3963b05974a to your computer and use it in GitHub Desktop.
Save AndriiBozh/e98687c4577044c5526cd3963b05974a to your computer and use it in GitHub Desktop.
Confirm the Ending (freeCodeCamp challenge)
/*
Check if a string (first argument, str) ends with the given target string (second argument, target).
This challenge can be solved with the .endsWith() method, which was introduced in ES2015.
But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
*/
function confirmEnding(str, target) {
var lastLetter = str.substr(-1); //get last character of str by its index (-1)
var lastWord = str.substr(str.lastIndexOf(target)); // returns the characters in a string beginning at the specified location through the specified number of characters
//str.lastIndexOf returns indices of (target) from a str.
if (lastLetter == target || lastWord == target){
return true;
}
else {
return false;
}
}
confirmEnding("are frozen", "n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment