Tell us what’s happening:
I’m aware of using the .slice() formula, but I prefer doing everything manually. This is the code that I’ve written so far. Every test case has been passed apart from the last console.log() statement. Could someone please point the error out for me? Your code so far
function confirmEnding(str, target) {
let tempString = []; tempString = str.split(" ");
let tempTarget = []; tempTarget = target.split(" ");
for (let i = 0; i < tempString.length; i++) {
if (tempString[tempString.length - tempTarget.length] == tempTarget[i]) {
return true;
}
else {
}
for (let j = 0; j < tempString[i].length; j++) {
if (tempString[i].substring(tempString[i].length - target.length) == target) {
return true;
}
else {
return false;
}
}
}
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.
Since there is no defined length of either of the strings- both str and target- how am I supposed to compare their ending words/ characters without using any built-in formula. @JeremyLT
Yeah, all of that is fine, but am I going in the right way with the code? Instead of splicing the strings into arrays, I can simply use the .substring() method, right? @JeremyLT
This seems like a valid approach! I think I get what you’re saying. Let me try it out!
My mind was honestly toast after getting stuck on this problem for hours. @JeremyLT
You have a loop, but you aren’t doing anything with the loop. You return from the loop on the first iteration, and the loop control variable is never used inside of the loop.
Okay, so now I know how to approach this using the .substring() method. Let me implement a for loop, then. Please give me a hint, real quick; should I use a single for loop or a nested for loop?