Created
January 11, 2018 22:10
-
-
Save AndriiBozh/e98687c4577044c5526cd3963b05974a to your computer and use it in GitHub Desktop.
Confirm the Ending (freeCodeCamp challenge)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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