Confirm the ending

@Beekey @Ediur your code works great! I battled with this problem for a while! I would like to ask if it is possible for you to explain the code to me, please? My confusion stems from the fact that str.substr returns an int as does target.length yet they pass the test cases! what I’m I missing here?

Hi Aldon2,

str.substr returns a string, target is also a string. We compare them with the logical operator === and get either true or false as a result which is then returned.

target.lenght is an int and it’s used within the substr prototype to isolate the correct substring of the given str.

Hope that helps :slight_smile:

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor

str=str.toLowerCase();
target=target.toLowerCase();
var ninja=str.split("");
var hello=target.split("");

ninja=ninja.reverse();
hello=hello.reverse();

for(var i=ninja.length;i>hello.length;i–)
{
ninja.pop();

}  

ninja=ninja.join(’’);
hello=hello.join(’’);

if(hello===ninja)
{return true;}
else{return false;}
return ninja;

}
confirmEnding(“He has to give me a new name”, “name”);

//try this code

1 Like

This is the very simple solution I came up with-

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  var subStr = str.substring(str.length-target.length);
  return subStr===target;
}

confirmEnding("Bastian", "n");
2 Likes

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var count=0;
for(var i=0;i<target.length;i++)
{
if(str.substr(str.length-i)===target.substr(target.length-i))
{
++ count;
}

  else 
    break;
}

if(count<target.length)
return false;
else if(count===target.length)
return true;
else
return false;
}

confirmEnding(“Bastian”, “n”);

***confirmEnding(“Connor”, “n”) should return false. —only this statement runs wrong.help!!

i did it this way…

function confirmEnding(str, target) {
// "Never give up and good luck will find you."
var compare = str.substring(str.length - target.length);
if (compare == target){
return true;
} else {
return false;
}
// – Falcor
//return str;
}

confirmEnding(“Bastian”, “n”);

i use slice ()

function confirmEnding(str, target) {
  var num = str.length-target.length;
  var newString=str.slice(num);
 return newString == target;
   
}
1 Like

Hello

Could someone tell me why this doesn’t split the Bastian into an array with its letters separate?

function confirmEnding(str, target) {
var array = [];
array = str.split(’ ');

// “Never give up and good luck will find you.”
// – Falcor
return array;
}

confirmEnding(“Bastian”, “n”);

question, why did you make

var strLen = target.length;

instead of just making strLen = -1?

thanks

oh, and i see that there is no need to make an array in the first place.

Here is my solution. Wanted to use both methods and all the rules with the numbers that go with it.

function confirmEnding(str, target) {

      if(target.substring(0) === str.substr (-target.length)){
        return true;
      }
      else
        return false;
      
    }
    confirmEnding("Bastian", "n");

Check below code: It’s working fine

  var lastString = str.slice(str.length-target.length);
  return lastString == target;

I’ve made it like that:

function confirmEnding(str, target) {
return str.substr(str.length - target.length) === target;
}

confirmEnding(“Bastian”, “n”);

Hello friends here is my code, i did it, i am so happy, i was 3 hours thinking, its alive, its alive


function confirmEnding(str, target) {
var name = str.split(" ");
if(name.length===1){
var name1 = String(name);
var theLast = name1.substring(name1.length-1);
if(theLast===target){
return true;
}else{
return false;
}
/-----------------------------------------/
}else{
var word = name[name.length-1].substring(2,6);
if (name[name.length-1] === target){
return true;
}else if(word === target){
return true;
}else{
return false;
}

}

}

confirmEnding(“Open sesame”, “same”);

Hi, I tried to complete this is the simplest way possible (I was getting annoyed with loops being used for these challenges) I think it should work for most cases. =)

function confirmEnding(str, target) {

return str.substr(-target.length,target.length) === target;

}

confirmEnding(“Bastian”, “n”);

Hello, i found some thing ‘ez’ to do with simple array. It just compare each letter backward.
Make sure to have the length - 1 because of array index. If some thing is wrong, return false. If nothing wrong found in the foreach, it’s true.


function confirmEnding(str, target) {
	let targetLL = target.length;
	let strL = str.length - 1; //7 => 6 in array index
	let targetL = target.length - 1; //1 => 0 in array index
	
	for( let k = 0; k < targetLL; k++ ) {
		if( str[strL] !== target[targetL] ) {
			return false;
		}
		
		strL--;
		targetL--;
	}
	
	return true;
};

console.log( confirmEnding("Bastian", "n") );
console.log( confirmEnding("Congratulation", "on") );
console.log( confirmEnding("Connor", "n") );
console.log( confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") );
console.log( confirmEnding("He has to give me a new name", "name") );
console.log( confirmEnding("Open sesame", "same") );
console.log( confirmEnding("Open sesame", "pen") );
console.log( confirmEnding("Open sesame", "game") );
console.log( confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") );
console.log( confirmEnding("Abstraction", "action") );

Hi guys ,
this is just a simple Code for beginning //

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
for (i=0; i<str.length;i++){
if (target == str.substr(-i) || target == str.substr(i) ) {
return true;}

}
return false;

}

confirmEnding(“Bastian”, “n”);

Hi All,
This is my code :

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var last_k="";
var str_map = str.split(" “);
var arr_string = str_map.length;
console.log(arr_string);
if(arr_string<2){
last_k = str_map.join(”").substr(-1);
if(last_k === target){
return true;
}else {
return false;
}
}else if(arr_string>=2){

  last_k= str_map[str_map.length-1];
console.log (last_k);
 if(last_k===target){
    return true;
  }else {
  return false;
  }
}

//return str_map;
}

confirmEnding(“He has to give me a new name”, “name”);
It’s working ok, with all of the situations, unless this : confirmEnding(“Open sesame”, “same”);

Some can help me? It’s an error of my code?

I’m guessing you are comparing the last word of the string to the target ans thus it is failing as even a match to the last portion of a word to the target should return true.

What I did for situations like this was to make a variable set to the length of the target:

var targetSize = target.length;

I then used

str.slice(-targetSize)

and checked how it compared to the target and if matched return true and if not, false.