$%#Confirm the Ending$#%

$%#Confirm the Ending$#%
0.0 0

#1

Tell us what’s happening:
i have no idea why it doesn’t work !

Your code so far

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  var nwstr = str.split("");
  if ( str.substr(nwstr.length-1-target.length ,  nwstr.length-1 ) === target) 
    {
      return true;
   
    }
    return false;
  
}

confirmEnding("Bastian", "n");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:


#2

You’re misunderstanding how to use substr. The first argument is the starting point. Your math is a bit off there. And the second argument is the is the length you want to select. Isn’t the length you want to select the length of the target?


#3

This says you’re looking for a property literally called “length-1-target”. You need to review using square brackets to access properties, eg someStr[someStr.length - 1]


#4

No, it evaluates to the same as nwstr.length - 1 - target.length. To access property names containing hyphens, you need to use bracket notation.

const obj = {
  'a': 0,
  'a-1': 0
}

obj.a-1 //=> -1
obj['a-1'] //=> 0

#5

You do not use split() because String object has length property.
You omit -1 in the expression.


#6

Ah crap, you’re right I was half asleep this morning.


#7

Thanks! i’ve done it, a little confuse at first argument :slight_smile:


#8

Thanks! you were right, i used to think the second argument was the ending point :smile: