# One test doesnt check for some reason

**URL:** <https://forum.freecodecamp.org/t/one-test-doesnt-check-for-some-reason/442334>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 23, 2021, 10:14pm UTC](https://forum.freecodecamp.org/t/one-test-doesnt-check-for-some-reason/442334 "2021-01-23T22:14:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gaa/32/193740_2.png) [@Gaa](https://forum.freecodecamp.org/u/Gaa)\
**Post date:** [January 23, 2021, 10:14pm UTC](https://forum.freecodecamp.org/t/one-test-doesnt-check-for-some-reason/442334/1 "2021-01-23T22:14:41Z")

</div>

Hey guys!  
This loop i just wrote has checked every test except the following:  
`confirmEnding("Open sesame", "same")` should return true.  
for some the if statement isnt executing. Why? The following tests are almost exactly the same but false (targets “game” and “sage”) and they checked, so i asume they do return the boolean after executing.  
Another thing i’d like to ask is about the code itself. I checked the solution to see if the problem was something simple that i was missing and it made me realize that my code is kinda wacky compared to the very simple example. Should i be worried about it?

```js

function confirmEnding(str, target) {
let i = 0;
while (i <= str.length - target.length) {
  if (i === str.length - target.length) {
    let ya = str.split(str[i-1]);
    let hopeThisWorks = ya.pop();
    console.log(hopeThisWorks);
    return hopeThisWorks == target;
  }
  i++;
}

}
confirmEnding("Open sesame", "same");

```

**Your browser information:**

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

**Challenge:** Confirm the Ending

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 23, 2021, 11:58pm UTC](https://forum.freecodecamp.org/t/one-test-doesnt-check-for-some-reason/442334/2 "2021-01-23T23:58:39Z")

</div>

I’m not sure I understand the logic of your approach.

You use a loop to figure out the different between _str.length_ and _target.length_. Can’t we just calculate that?

```auto
let ya = str.split(str[i - 1]);

```

Why do we want to split it along the char at that char? In this case it is the letter “e”. There are are 3 of those in there. I see that you are trying to break off the end of _str_ that is the same length as _target_ - that is a good approach. I just think you can find an easier way to do it. Do you know how to break strings into substrings?

```auto
let hopeThisWorks = ya.pop();

```

Of course, because of how you’ve split your strings, this is giving you and empty string.

I think you need to rethink your algorithm. If you were to explain how to do this to someone, check if the end of a word matches the target string, how would you tell them to do it. Don’t think about computer languages, just the algorithm. Whatever you come up with, JS probably has a way to do it.

I can think of two simple ways to do this. The first one is the one that you appear to be trying but it doesn’t need a loop. The other way uses a loop, but in a different way.

---

<div class="post-metadata">

**Author:** ![Gaa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gaa/32/193740_2.png) [@Gaa](https://forum.freecodecamp.org/u/Gaa)\
**Post date:** [January 24, 2021, 12:41am UTC](https://forum.freecodecamp.org/t/one-test-doesnt-check-for-some-reason/442334/3 "2021-01-24T00:41:41Z")

</div>

I dont know why i thought that using split() with bracket notation there would separate by the index and not the character in there. Thanks so much for answering!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [July 25, 2021, 12:41pm UTC](https://forum.freecodecamp.org/t/one-test-doesnt-check-for-some-reason/442334/4 "2021-07-25T12:41:50Z")

</div>

Closed on July 25, 2021, 12:41pm UTC

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