Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challenges 
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-05-11 19:22:51

---
title: Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challenges 
published: true
description: My guide, notes, and solution to FreeCodeCamp's basic algorithm challenge, "Confirm the Ending"
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-006-confirm-the-ending
series: Solving freeCodeCamp's Algorithm Challenges
---

Post can also be found on my website, [https://virenb.cc/fcc-006-confirm-the-ending](https://virenb.cc/fcc-006-confirm-the-ending)

!["Confirm the Ending" Problem](//images.ctfassets.net/yzwpq1epaq68/59uxXpJ219XlKKpVxw06JQ/ec907c261309b9f6ee606fcb08a2e1a4/ss-006.png)

Let's solve freeCodeCamp's Basic Algorithm Scripting Challenge, "Confirm the Ending"

## Our Starter Code (& Tests)
```javascript
function confirmEnding(str, target) {
  return str;
}

confirmEnding("Bastian", "n");
```
```
// Tests
confirmEnding("Bastian", "n") should return true.
confirmEnding("Congratulation", "on") should return true.
confirmEnding("Connor", "n") should return false.
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
confirmEnding("He has to give me a new name", "name") should return true.
confirmEnding("Open sesame", "same") should return true.
confirmEnding("Open sesame", "pen") should return false.
confirmEnding("Open sesame", "game") should return false.
confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
confirmEnding("Abstraction", "action") should return true.
Your code should not use the built-in method .endsWith() to solve the challenge.
```

### Our Instructions


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

This challenge _can_ be solved with the `.endsWith()` method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

---

## Thoughts

* We have two inputs, both being string data types
* Looking at the tests, our output must be a boolean value (true or false)
* Do not use `.endsWith()` method in our solution
* What we need to do is compare two strings


### Further Thoughts

Our first input, `str`, is always longer than the second input, `target`. `str` is normally a word or a sentence. 

Strings have a few properties and methods. `String.length` tells us how long our string is. 

A method that would help us solve this is `String.substring()`. It will return a part of the string, depending on what index arguments you provide. It takes in at least one argument, `String.substring(indexStart[, indexEnd])`, the `indexEnd` being optional.

**_So we can subtract the `target` length from the `str` length, we will get the `str` value from the index where we want to compare._**

Let's look at that in some code.

```javascript
function confirmEnding(str, target) {
  let strLength = str.length;	// 7 since str is "Bastian"
  let targetLength = target.length // 1 since target is "n"
  return str.substring(strLength - targetLength) // From the 6th index, this method will return "n"
}

confirmEnding("Bastian", "n");
``` 

**Reference:** [MDN Documentation - String.prototype.substring()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring "String.prototype.substring() Documentation")

If the 6th index in "Bastian" is "n", the above will return `true`. 

```
str[0] = "B"
str[1] = "a"
str[2] = "s"
str[3] = "t"
str[4] = "i"
str[5] = "a"
str[6] = "n"
``` 

---

## Solution

<details>
  <summary>**[SPOILER: SOLUTION TO CODE BELOW]**</summary>
 	
  ```javascript
  function confirmEnding(str, target) {
    return str.substring(str.length - target.length) == target;
  }
  ```
 
</details>

---

### Links & Resources

[Confirm the Ending Challenge on FCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending "Basic Algorithm Scripting: Confirm the Ending Challenge on FCC Website")

[FreeCodeCamp](https://www.freecodecamp.org/, "FreeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "FCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/006---confirm-the-ending.js "GitHub Solution")

Thank you for reading!