-
Notifications
You must be signed in to change notification settings - Fork 0
Algorithm Confirm The Ending
The function is a whole Boolean operation. You need to return true if the first argument ends with the second argument. This means that for the problem script, it should return true for the confirmEnding('Bastian', 'n'); case.
Take a look at how substr() works. You will be trying to get the last Nth characters.
To get the Nth-to-Last character you will use length() and turn it into a negative number.
Check that you have the proper syntax and that you use === to compare.
Solution ahead!
function confirmEnding(str, target) {
return str.substr(-target.length) === target;
}🚀 Run Code
We use the subtring() with the negative value of the lengths of target. We could use -1 to get the last element but if the target is actually longer than one letter then the program will provide the wrong information. Then we return the value of this boolean expression.
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 for your help with Algorithm: Confirm the Ending
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links
