From bb7140539fcb58588141a5e762272fb48efbd37c Mon Sep 17 00:00:00 2001 From: Matt Geurian Date: Mon, 13 Feb 2017 10:34:11 +0900 Subject: [PATCH] Update 07_confirm_ending.js --- Basic/07_confirm_ending.js | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/Basic/07_confirm_ending.js b/Basic/07_confirm_ending.js index dbf5e8f..743e88b 100644 --- a/Basic/07_confirm_ending.js +++ b/Basic/07_confirm_ending.js @@ -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"));