Confirm the Ending of the string

I need help understanding why my code doesn’t work.

code is given below.

function confirmEnding(str, target) {
  let newString = ' ';
  if (target.endsWith('n')) {
    newString = target;
    return true;
  }
  return false;
}

confirmEnding("Bastian", "n");

The link of the challenge is also given below.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending

This is true

function confirmEnding(str, target) {
  let newString = ' ';
  if (str.endsWith(target)) {
    newString = target;
    return true;
  }
  return false;
}

but you get this error

Do not use the built-in method .endsWith() to solve the challenge.

So you need an other point of view.

@OJL brother, is my given code correct, if built in method was allowed to use in the challenge?

You are not far :
this work :

function confirmEnding(str, target) {
  if (str.endsWith(target)) {
    return true;
  } else {
   return false;
}
}

Ahh I see…
lets try with another approach. :slight_smile:

Hi

try this beautiful approach using regex :

function confirmEnding(str, target) {
let patt = new RegExp(target+ "$");
return  patt.test(str); 
}

@OJL please check the code below. why isn’t that running. Do I need to make a regex first?

function confirmEnding(str, target) {
  let check = /str$/;

  if (check == target){
    return true;
  }
  return false;
}

confirmEnding("Bastian", "n");
function confirmEnding(str, target) {
  let check = new RegExp(target + "$");
  return check.test(str);
}

@OJL I’ve checked the previous Regex coding challenges in which we were taught this method given below.

function confirmEnding(str, target) {
  let regex = /str$/;
  let result = regex.test(str);
  return result;
}

confirmEnding("Bastian", "n");

In your code below please define what is new keyword and RegExp(target+ “$”);

function confirmEnding(str, target) {
let patt = new RegExp(target+ "$");
return  patt.test(str); 
}

Hello
I had take a look at your activity in FCC forum , I don’t expect this kind of questions from one in your level.

@OJL I am a newbie programmer not a pro, learning to code. All previous challenges are mixed in my head.

Hi
I’m also beginner, but try to finish the current chapter and understand the concept behind before moving to the next , especially when we are learning the basis.