Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions Basic/07_confirm_ending.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
// YouTube: https://youtu.be/AsLtWDncqeQ

function confirmEnding(str, target) {
str = str.toLowerCase().replace(/\W_/g, "");
return target === str.slice(-Math.abs(target.length));

return target === str.substring(str.length - target.length);
}

/* You can replace str.slice() with str.subtr()
and the result will be exactly the same.
What is the difference?
/* I've used the .substring method and the length property to extract the last
characters of each string. Nice and neat in one line.

Both .slice() and .substr() take one required argument, which is the starting index.
If you don't include the second optional argument, it selects everything
from the start position to the end of the string.

They also take an optional second argument, and this is where they differ.

For .slice(start, end), the second argument is the end index number.
It will select everything up to the end index, but it won't actually include
the character at the end index.

For .substr(start, length), the second arugument is the length of your selection.
So if you write str.substr(2,4), it will select 4 characters beginning at index 2.

*/

console.log(confirmEnding("Bastian", "n"));
console.log(confirmEnding("He has to give me a new name", "name"));
Expand Down