Confirm the Ending - algorithm

Tell us what’s happening:

I was doing this practice and I finished it but i fill like my code is too long. I am pretty sure there is a even more simpler algorithm to complete this challenge. I would much appreciate it if anyone would tell me a simpler algorithm for this challenge.

And i am also really sorry for not naming my variables properly.

Your code so far

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor;
var f = str.split(’’);
var l=’’;
var k = 1;
for (var i = target.length;i>0;i–){
l+=f[(str.length-k)]
k++;
}
var n=l.split(’’);
n.reverse();
var u = n.join(’’);
if(target==u){
return true;
}else{
return false;
}

}

confirmEnding(“Bastian”, “an”);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending/

You might consider looking at String.prototype.slice to remove characters from str for comparison to target.

thanks for your help now my code got way smaller than before:

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

}

confirmEnding(“Bastian”, “an”);

You’re welcome. I was tempted to show you a complete solution but you seemed curious so I just gave you that clue and let you figure it out.

If you look down the left side of that MDN page there are more useful String methods listed.

Since yours are both working solutions can you edit your posts to wrap your code in spoiler tags? It keeps others from seeing a full solution if they are just looking for hints.

[spoiler]

This code will be blurred

[/spoiler]

Can’t see this accidentally

1 Like