What's wrong in this code?

console.log(confirmEnding(“Connor”, “n”));
returns true , Connor have two n’s and my function returns true but it should be false bcz n should be match only one time not twice according to the challenge . So tell me how to do it ?

  **Your code so far**

function confirmEnding(str, target) {
var string= [];
string.push(str)
var result;
for(let i=0;i<string.length;i++){
if(string[i].includes(target)==true){
  result = true;
}
else{
  result  = false;
}
}
return result
}
console.log(confirmEnding("Connor", "n"))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36

Challenge: Confirm the Ending

Link to the challenge:

includes is a very useful function, but not necessarily for this. Instead, can you think of some string functions that might let you pull a smaller string from a bigger one? A substring of str, if you will?

1 Like

it seems you have misinterpreted the instructions, you are checking the ending of the string, the last character(s) of str must be equal to target for the function to return true

1 Like

Nope I totally understand the instructions . Actually I took the wrong approach for this question.

1 Like

Did you get it working?? Wildly curious now lol

:sweat_smile: still trying to find the solution… I’ll sent u a message when it’s done

1 Like

This isn’t the solution, but it might help: DevDocs

confirmEnding("Connor", "n") should return false because Connor doesn’t end with an “n”, not because Connor has two n instead of one

2 Likes

Okkk now I get it . I actually misinterpreted the instructions :man_facepalming:

1 Like

The problem per me, is when you said string.push(str) this means string is and array of one string value, ie the str is not converted to array.

also .includes() method is a recursive itself, it goes through the string. completely character by character.

So you don’t need

  • the for(){} loop, you don’t
  • no need also making the str an array due the different methods strings have

Well:
string.includes(stringToSearch, startSearchAt);

I hope this explains itself

Mod Edit: Solution Redacted

1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

1 Like

Oh, my bad - how can I remove the picture please?