Oletem
10/4/2017 - 8:55 PM

Confirm the Ending

Check if a string (first argument, str) ends with the given target string (second argument, target).

This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

String.prototype.substr() String.prototype.substring()

/*------------------------------------------------Simplest using substring and length----------------------------------------------------------*/

function confirmEnding(str, target) {
  
  

  
  return str.substr(str.length - target.length) ==target; /*То же самое, что задать минусовое начало start
Location at which to begin extracting characters. If a negative number is given, it is treated as strLength - start where strLength is the length of the string. For example, str.substr(-3) is treated as str.substr(str.length - 3)
length. The number of characters to extract. If this argument is undefined, all the characters from start to the end of the string are extracted.
  То есть счет идет не с конца, а таки с всего количества знаков в стринге - таргет. То есть
  
  в этом месте, на 24 позиции в "He has to give me a new name" - ноль. Послкольку аргумент конца не укзаан(второй)
  то метод берет все знаки до конгца стринга.
  
  The substr() method returns the characters in a string beginning at the specified location through the specified number of characters. 

SyntaxEdit

str.substr(start[, length])
Parameters

start
Location at which to begin extracting characters. If a negative number is given, it is treated as strLength - start where strLength is the length of the string. For example, str.substr(-3) is treated as str.substr(str.length - 3)
length
The number of characters to extract. If this argument is undefined, all the characters from start to the end of the string are extracted.
Return value

A new string containing the extracted section of the given string. If length is 0 or a negative number, an empty string is returned.
  
  */
  
}

confirmEnding("He has to give me a new name", "name");
/*                                   ///////////////////////////////////////////////////////*/
Using substring() with length property
The following example uses the substring() method and length property to extract the last characters of a particular string. This method may be easier to remember, given that you don't need to know the starting and ending indices as you would in the above examples.

// Displays 'illa' the last 4 characters
var anyString = 'Mozilla';
var anyString4 = anyString.substring(anyString.length - 4);
console.log(anyString4);

// Displays 'zilla' the last 5 characters
var anyString = 'Mozilla';
var anyString5 = anyString.substring(anyString.length - 5);
console.log(anyString5);

/*-------------------------------------------------------------------------------------------------*/

function confirmEnding(str, target) {

  
return str.substr(-target.length)=== target;/*(-target.length) - target - "mountain"(8знаков) 

при отрицательном аргумент str.substr считает не с нуля, а с конца стринга, то есть

в нашем случае - от конца отсчитывает длинну - 8 знаков, их мы и сравниваем. str.substr(a,b) - 

берет первый аргумент началом подреза, второй - аргументом конца(невключительно). Вроде, сюда не очень подходит*/
  
}

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain");

/*-----------------------------------------------------------------------------------------------------*/

function confirmEnding(string, target) {
  if (string.substr(-target.length) === target) {
    return true;
  } else {
    return false;
  }
}
confirmEnding('Bastian', 'n');

function confirmEnding(string, target) {
  return (string.substr(-target.length) === target) ? true : false;
}
confirmEnding('Bastian', 'n');

/*------------------------------------------Confirm the Ending of a String With Built-In Functions — with endsWith()------------------------------------------------*/


function confirmEnding(string, target) {
  // We return the method with the target as a parameter
  // The result will be a boolean (true/false)
  return string.endsWith(target); // 'Bastian'.endsWith('n')
}
confirmEnding('Bastian', 'n');