Basic Algorithm Scripting - Coding Challenge

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.

Challenge: Confirm the Ending

Link to the challenge:

Why are you splitting on spaces? I think this is making your code harder than it should be.

I’m converting the string into an array of strings and then trying to compare the elements of the array with that of the target string.
@JeremyLT

But why? You just need to know if the end of str matches target. How does splitting help you do that?

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

The length of a string is a defined property.

Is there any way of approaching this challenge without employing the slice() method? Just for loop(s); that’s it!

Yes. But you have to use the .length property of the strings.

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

Sure, you could use .substring() or you could use a raw for loop over the length of the target string.

1 Like

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

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

Okay, so @JeremyLT, this is the best that I could come up with. What do you think, buddy?

I’m still not sure why you are splitting on spaces.

My bad! That was just a redundant piece of code.

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.

You’re right; that’s also a useless loop then.

So you can either approach this with .substring() or a for 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?

You should only need a single loop.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.