Confirm the Ending of the string

Confirm the Ending of the string
0

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.

@oussama-jlassi 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); 
}

@oussama-jlassi 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);
}

@oussama-jlassi 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.

@oussama-jlassi 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.