Very confused why this code passes all tests except one

Tell us what’s happening:
This code passes all the tests except for only one, the “Open sesame” with target “sage” test.
Especially confused because the code passes “Open sesame” with the “same” and “game” targets but only not “sage”, as if the “g” is equal to “m” to the computer.

Your code so far


function confirmEnding(str, target) {
str = str.split('');
target = target.split('');
let newStr = str.slice(str.length - target.length)

console.log(newStr)
console.log(target);

for (let i = 0; i < newStr.length; i++) {
  if (newStr[i] === target[i]) {
    return true;
  } else { 
    return false;
  }
}
}
confirmEnding("Open sesame", "sage");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36.

Challenge: Confirm the Ending

Link to the challenge:

So your loop:

  for (let i = 0; i < newStr.length; i++) {
    if (newStr[i] === target[i]) {
      return true;
    } else {
      return false;
    }
  }

It is really only checking the first letter. In your failing example, you are comparing the strings “sage” and “same”. So, when you start the loop, you are checkout the first char of each, both “s”. In your if statement, you compare them, they match, so you return true. You return from the entire function, that’s it, no more checking, it’s done.

You need to figure out a different plan there. Try to think through what you want it to do, how to check ones with more than one character. Mentally step through how you would do it, char by char.

2 Likes

I was able to get it to work by adding one line and changing another. (Or you could think of it as moving one line and replacing it with another.) There’s more than one way to do it, but that worked for me. Let us know if you need more of a hint.

wow awesome. thank you so much!

thanks, a million for the fast response! I think I got it now, I need to move the return outside of the loop!

Well, there are different ways to do it. I was imagining moving one of the returns outside the loop. I guess what I wasn’t thinking was the best - instead of replacing that line I should have deleted a few. But whatever works. The point is that there needs to be some kind of return outside the loop. And you may or may not have a return inside the loop - the important thing there is that for that condition you need to stop the loop - there are few ways to do that.

thanks I hear ya, understanding more of what you are saying as I figure it out now. Also I do realize the code is longer than it has to be and can definitely be more succinct, its just that as I am still a beginner it helps me to break it down into multiple steps at first, which I can then cut out the extra steps after once I get it to work.

…the code is longer than it has to be and can definitely be more succinct, its just that as I am still a beginner…

Absolutely, 100%.

Figured out one solution using a loop!

function confirmEnding(str, target) {
str = str.split('');
target = target.split('');
let newStr = str.slice(str.length - target.length)

console.log(newStr)
console.log(target);

for (let i = 0; i < newStr.length; i++) {
  if (newStr[i] !== target[i]) {
    return false;
  }  
}
  return true;
}
confirmEnding("Open sesame", "sage");

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I also put [spoiler] tags around it since it is a working solution.

Yeah, that’s kind of what I was thinking about. Good job.

Only real nitpick I would have is that I don’t think we need to break these into arrays - the original strings should already have the slice method and length property and should work fine if we remove those two lines. There are some other improvements, but I don’t want you to get bogged down on this one.

But still, good job, good luck on the next one.

1 Like