Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

2.6 KiB

title
title
Confirm the Ending

Confirm the Ending


Solutions

Solution 1 (Click to Show/Hide) (Declarative approach)
function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor

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

confirmEnding("He has to give me a new name", "name");

Code Explanation

  • First we use the slice method copy the string.
  • In order to get the last characters in str equivalent to the target's length we use the slice method.
  • The first parameter inside the slice method is the starting index and the second parameter would be the ending index.
  • For example str.slice(10, 17) would return give me.
  • In this case we only include one parameter which it will copy everything from the starting index.
  • We substract the length of str and the length of target, that way, we shall get the last remaining characters equivalent to the target's length.
  • Finally we compare the return result of slice to target and check if they have the same characters.
Solution 2 (Click to Show/Hide) (using Regular Expression)
function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor

  let re = new RegExp(target + "$", "i");

  return re.test(str);
}

console.log(confirmEnding("Bastian", "n"));

Code Explanation

  • We need to make a pattern from the target variable that exists at the end of the string str.
  • Since we will use a variable that will change the pattern each time the function is called, we will use the constructor of the regular expression object new RegExp(pattern[, flags]), so we start with: new RegExp(target).
  • Then we have to check at the end of the string, so we concatenate to the target variable the $ character to match the end: new RegExp(target+'$').
  • We use the flag i to ignore the case of the pattern and we have our completed RegExp: new RegExp(target+'$','i'), or we can ommit the flag entirely.
  • Finally, we are using our regular expression with the test method to the given string, to check if the string ends with the pattern and return true or false accordingly.