Tests written are wrong

Tell us what’s happening:
The tests for this exercise is written wrong I assume

  **Your code so far**

function confirmEnding(str, target) {
let isPresent;

for(let i = 0; i < str.length + 1; i++) {
  console.log(str.substring(i, target.length + i))
  if(str.substring(i, target.length + i) === target) {
    isPresent = true;
    break;
  } else isPresent = false;
}

console.log(isPresent)

return isPresent;
}

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification");
  **Your browser information:**

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

Challenge: Confirm the Ending

Link to the challenge:

Could you explain why you think tests are wrong?

As @sanity asked, why would you assume it is the tests that are broken? These tests have been run millions of times. I no coding is frustrating - it is for everyone. But don’t get in the habit of assuming that the fault lies somewhere else. We all do it, but it is a bad habit. Ultimately, computers do exactly what we tell them to.

But it’s easy to check. The first failing test says:

confirmEnding(“Connor”, “n”) should return false.

When I test that with:

confirmEnding("Connor", "n") should return false.

I get true.

Therefore I would say that the test is working correctly. Your function should return false because “Conner” does not end with “n”. But it returns true.

1 Like

As an example, if I put in some log statements, I can watch what is happening:

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")

gives me:

comparing ***Walking on wa***specification***
comparing ***alking on wat***specification***
comparing ***lking on wate***specification***
comparing ***king on water***specification***
comparing ***ing on water ***specification***
comparing ***ng on water a***specification***
comparing ***g on water an***specification***
comparing *** on water and***specification***
comparing ***on water and ***specification***
comparing ***n water and d***specification***
comparing *** water and de***specification***
comparing ***water and dev***specification***
comparing ***ater and deve***specification***
comparing ***ter and devel***specification***
comparing ***er and develo***specification***
comparing ***r and develop***specification***
comparing *** and developi***specification***
comparing ***and developin***specification***
comparing ***nd developing***specification***
comparing ***d developing ***specification***
comparing *** developing s***specification***
comparing ***developing so***specification***
comparing ***eveloping sof***specification***
comparing ***veloping soft***specification***
comparing ***eloping softw***specification***
comparing ***loping softwa***specification***
comparing ***oping softwar***specification***
comparing ***ping software***specification***
comparing ***ing software ***specification***
comparing ***ng software f***specification***
comparing ***g software fr***specification***
comparing *** software fro***specification***
comparing ***software from***specification***
comparing ***oftware from ***specification***
comparing ***ftware from a***specification***
comparing ***tware from a ***specification***
comparing ***ware from a s***specification***
comparing ***are from a sp***specification***
comparing ***re from a spe***specification***
comparing ***e from a spec***specification***
comparing *** from a speci***specification***
comparing ***from a specif***specification***
comparing ***rom a specifi***specification***
comparing ***om a specific***specification***
comparing ***m a specifica***specification***
comparing *** a specificat***specification***
comparing ***a specificati***specification***
comparing *** specificatio***specification***
comparing ***specification***specification***
match found, breaking out of for loop
true

Did we want to break at that point? We are setting isPresent to true. And this does check if it is present. But we don’t care about that - we care about the ending. We only care if the ending matches. The instructions say:

Check if a string (first argument, str ) ends with the given target string (second argument, target ).

You don’t need a for loop for this, because we don’t need to check every combination. We only need to check the ending.

1 Like
function confirmEnding(str, target) {

let isPresent;

for(let i = 0; i < str.length; i++) {

  console.log(str.substring(i, target.length + (i + 1)))

  if(str.substring(i, target.length + (i + 1)) === target) {

    isPresent = true;

    break;

  } else isPresent = false;

}

console.log(isPresent)

return isPresent;

}

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification");

//If u leave (i, target.length + i) u are basically telling ur code to stop looping whenever it finds a match…

// In the case of (“Connor”, “n”) it loops until n and then it tells u yoo I found ‘n’ its true when in reality u are loooking if the target matches the end of your string

// Here same thing (“Walking on water and developing software from a specification are easy if both are frozen”, “specification”) it loops and then it finds ‘specification’ and the says yoo I found it and soo its true when its not because u were trying to see if the last matches the target.

// Now if u change it to (i, target.length + (i + (1 OR ANY POSITIVE NUMBER)) when u add 1 or any positive number it works because u are telling it to only compare the last thing it finds.

I am so sorry to be accusing of that fact, I thought that target should be present in the sentence/ word instead of target ending in the given sentence/word.

Thanks for helping out!

Hello there.

Thank you, for your contribution. For future contributions, please wrap your solution within :

[details]
```
code goes here...
```
[/details]

Also, provide all of the necessary code to pass the challenge.

Also, 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 (’).

Hey Sorry man, I misinterpreted the question.

No, it’s 100% cool. We’ve all been there. Early on, there were countless times when I angrily came to the conclusion, “JavaScript must be broken, there is no other possible explanation”. And then some time later I was embarrassed to find out that it was in fact my code that was broken. It happens. You have to learn that lesson a few times.

The other lesson is to read things very closely. When I get a new task at work, I read through the specs a few times. If it is a multi-day task, I reread them as at the start of each day. When I finish the code, I reread them and then again before submitting. Paying attention to tiny details is part of the job.

I know both of these because they’ve bitten me in the ass more than once. It’s part of the learning process.

1 Like

And got the solution as well,

function confirmEnding(str, target) {
  let isPresent;

  let lastWord= str.substring(str.length - target.length, str.length)
  console.log(lastWord)

  if(lastWord === target) isPresent = true; else isPresent = false

  console.log(isPresent)

  return isPresent;
}

confirmEnding("Bastian", "n");

@GeoBrodas

We don’t like working answers out in the open so I hid yours behind [spoiler][/spoiler] tags.

But cool, you got it. Yeah, that looks a lot better.

I might just add that something like this:

if(lastWord === target) isPresent = true; else isPresent = false

is overly complicated. This would have the same effect:

isPresent = lastWord === target

In fact, at that point I wonder if that variable is even needed, and might just return that directly:

return lastWord === target

It works exactly the same but is a little less “busy” looking, cutting it down to just two lines. Sometimes you do want to take a few extra lines to do something to make it more readable, but I think it is overkill here.

Still, good job. It solved the problem. Have fun on the next one.