Confirm the ending in javascript

Mastering String Manipulation: Confirm the Ending Challenge in JavaScript

Introduction

Confirm the Ending! This is a minor coding challenge, but it's crucial to know this type of challenge, as they might be asked in interviews. So, keep yourself always ready. In this coding challenge, we will be given a function with two arguments: word/sentence and target. The first argument is a word or a sentence ('challenge', 'this is a challenge'), and the second argument is a signal character or more than one character ('e', 'ge'). Our job is to check whether our word/sentence ends with the same values as our given target values and output a boolean value true or false. If the word/sentence ending value is the same as target, then the output will be true; otherwise, false. It might seem very tricky, but don’t worry. This is just a summary of the challenge, we haven’t explored it in depth yet. We will explore it further by coding to clear all your doubts.

Remember that every programming language has countless ways to solve every challenge. I'm going to emphasize the way you can see below.

JavaScript

copy

 function confirmEnding(str, target) {
 if(str.split('').slice(str.length - target.length).join('') === target) {
   return true
 } else {
   return false
 }
 }
                
 console.log(confirmEnding("Bastian", "n"));
// output: true

Let’s break this down for better understanding!

Breaking Up Solution:

Have you seen the confirmEnding function above? It takes two arguments: str and target. It is a simple function, so I hope you already have some knowledge about it. str represents the word or sentence we are working with, while target represents the value we are looking for at the end.

As we have understood, the function works successfully. Now, it's time to start coding to solve it. I used the split(), slice(), and join() methods to solve this challenge within an if/else condition. See how simple it is.

Using the if/else condition is crucial because we need to return either true or false, and this can only be achieved by using a condition.

By using the split() method, we can access each character of the string. The split() method converts a string into an array, as shown below:

JavaScript

copy

 let str = 'challenge';
 console.log(str.split(''))
 // output:  [ 'c', 'h', 'a', 'l', 'l', 'e', 'n', 'g', 'e' ];

By using the slice() method, we can extract desired values from an array. It takes two arguments: the first argument specifies the index in the array from which to start the extraction, and the second argument specifies the index at which to stop the extraction (this index will not be included in the result). The second argument can be omitted, which is what we did here:

JavaScript

copy

 slice(str.length – target.length)
 //output: ['n']

Given that our string is Bastian and the target value is n, where the length of the string is 7 and the length of the target is 1, we use slice(0, 6) to slice the string. This will omit the first 6 characters, leaving us with the character we want: n. The purpose of this step is to obtain the last character or characters (with the same length as the target).

Although we have obtained the value we were looking for within an array, this is because we converted our string into an array using the split() method to implement the slice() method. Now, we have used the join() method to convert the array back into a string for efficient comparison.

JavaScript

copy

 console.log(['n'].join(''));
// Output: 'n'

We have successfully obtained the values we needed. Next, we use the === equality operator to compare whether the value we obtained is equal to the target value. If it is, we will print true otherwise, we will print false.

All Coding Challenges