# Basic Algorithm Scripting - Confirm the Ending

**URL:** <https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372>\
**Category:** I Built This\
**Created:** [November 2, 2022, 5:14pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372 "2022-11-02T17:14:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qlander](https://avatars.discourse-cdn.com/v4/letter/q/ba8739/32.png) [@Qlander](https://forum.freecodecamp.org/u/Qlander)\
**Post date:** [November 2, 2022, 5:14pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/1 "2022-11-02T17:14:47Z")

</div>

Im having some difficulty understanding the syntax of this solution and hoping for some clarity please. I read the relevant link on slice() but still have unanswered questions.

**THE TASK:** Check if a string (first argument, `str`) ends with the given target string (second argument, `target`).

Normally .slice() takes 2 arguments, a startIndex and stopIndex. But if you dont include the stopIndex, then everything gets copied.

This solution appears to copy the entire string then subtracts the target string. In this challenge, my first instinct is that that would mean 7 - 1. Especially since I thought .length always returned an integer.

Additional logic could suggest that this solution means “Bastian” - “n” === “Bastia”. But thats not whats going on here either.

Ive played around with the str and target values so I understand what the solution is doing, it just conflicts with my understanding of the .length syntax.

When does .length not return an integer? Im assuming it has something to do with using “-” with slice(). “-” must not mean subtract here.

```javascript
function confirmEnding(str, target) {
return str.slice(str.length - target.length) == target;
}

console.log(confirmEnding("Bastian", "n"));

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0`

**Challenge:** Basic Algorithm Scripting - 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:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 2, 2022, 5:19pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/2 "2022-11-02T17:19:52Z")

</div>

Again, easiest if you compare to the solution you wrote.

* * *

Anyways, looking at MDN

> **[String.prototype.slice() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice)**
>
> The slice() method of String values extracts a section of this string and
> returns it as a new string, without modifying the original string.

> `indexStart` - The index of the first character to include in the returned substring.

> `indexEnd` - The index of the first character to exclude from the returned substring.

What this says is not quite the same thing as

> [@Qlander](#):
>
> But if you dont include the stopIndex, then everything gets copied.

* * *

> [@Qlander](#):
>
> When does .length not return an integer?

It is returning an integer.

---

<div class="post-metadata">

**Author:** ![Qlander](https://avatars.discourse-cdn.com/v4/letter/q/ba8739/32.png) [@Qlander](https://forum.freecodecamp.org/u/Qlander)\
**Post date:** [November 2, 2022, 6:03pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/3 "2022-11-02T18:03:50Z")

</div>

If anyone else is monitoring this and can provide a tangible answer to my questions, I would be much obliged. Thank you.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 2, 2022, 6:06pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/4 "2022-11-02T18:06:59Z")

</div>

I provided a “tangible answer”.

`.length` does in fact return an integer.

Invoking `.slice()` with a single argument copies everything from the single argument’s value (`indexStart`) to the end of the string.

That index is the length of the `str`ing minus the length of the `target`.

So, that call to `.slice()` copies the last characters of `str`, up to the number of characters in `target`.

* * *

It is easier to explain when you compare with whatever solution you wrote because the quantity `str.length - target.length` is going to end up showing up in the solution somewhere.

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [November 2, 2022, 6:49pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/6 "2022-11-02T18:49:56Z")

</div>

> [@Qlander](#):
>
> This solution appears to copy the entire string then subtracts the target string. In this challenge, my first instinct is that that would mean 7 - 1. Especially since I thought .length always returned an integer.

I think this is where the confusion is. `str.slice(str.length - target.length)` isn’t copying the entire string. It’s passing the value of `str.length - target.length` as the first parameter to the slice call.

That is, `confirmEnding("Bastian", "n")` would result in `str.length` being 7, `target.length` being 1, and the result passed to `str.slice()` being `6`.

`str.slice(6)` then, would start at index 6 and take the rest of the string. In this case, index 6 would be `n`, which would match the value of `target`.

---

<div class="post-metadata">

**Author:** ![Qlander](https://avatars.discourse-cdn.com/v4/letter/q/ba8739/32.png) [@Qlander](https://forum.freecodecamp.org/u/Qlander)\
**Post date:** [November 2, 2022, 7:17pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/8 "2022-11-02T19:17:18Z")

</div>

Awesome! This really addressed my confusion and clarified things for me, I appreciate it. Thank you!

---

<div class="post-metadata">

**Author:** ![staranbeer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/staranbeer/32/327483_2.png) [@staranbeer](https://forum.freecodecamp.org/u/staranbeer)\
**Post date:** [November 3, 2022, 4:33am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/9 "2022-11-03T04:33:36Z")

</div>

Hey!

> [@Qlander](#):
>
> Please accept my formal request that you no longer respond or engage me from this point forward. I beg that I dont have to ask you again or interpret future contact as stalker and harassment behavior. Thank you

I just went through this thread and I couldn’t find anything that would require you to respond in this way.  
“Harrassment” as a word has a lot of weight to it and calling a moderater who’s been helping people on the forums for years is really inappropriate and doing this on a public thread that anyone can see is not something you should do “just because”.

---

<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:** [May 4, 2023, 4:34pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-confirm-the-ending/568372/10 "2023-05-04T16:34:09Z")

</div>

Closed on May 4, 2023, 4:34pm UTC

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