Confirm the Ending2

At start writes an error: “countTarget.join is not a function”. Although the function is written correctly.

function confirmEnding(str, target) {
var countTarget = ;
for(var i = 0; i < target.length; i++){
countTarget = i;
var number = countTarget.length;
}
var anyString = str.substring(str.length - number);
var answer;
if (anyString == countTarget.join(’’))
{answer = true;}
else {answer = false; }
return answer;
}
confirmEnding(“Bastian”, “n”);


**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 YaBrowser/19.6.2.599 Yowser/2.5 Safari/537.36`.

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending

countTarget is a number, when join() is an array method, if you try to use a method on a data type not compatible it will say it is not a function

Thank you very much.
Now the problem with the if statement. if I put the = sign, the condition is satisfied true. If i put sign == the condition is satisfied false

No, if you use = you are assigning what is to the right of it to the left of it, when you use == you are comparing the two things and is evaluated as a boolean

If you post new code I can see what you changed

function confirmEnding(str, target) {  
var countTarget = [];  
for(var i = 0; i < target.length; i++){  
countTarget = [i];  
var number = countTarget.length;  
} 
var anyString = str.substring(str.length - number);  
var answer;  
if (countTarget.join('') == anyString)  
{answer = true}  
else {answer = false}
return answer;  
}  
confirmEnding("Bastian", "n");

Now don’t return true

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Well, could you explain me what you want to do with each line of code? There are few weird things but I would need to be sure what you want to do with them

Ok. Thank you for your tip.

var countTarget = [];
array declaration

for(var i = 0; i < target.length; i++){ countTarget = [i];
countTarget = [i];  

iterates through the array, that to count target

var number = countTarget.length;

declaring a new variable and counting the number of elements in an array

var anyString = str.substring(str.length - number);

by the method substring to get a necessary piece str

if (countTarget.join('') == anyString)

and compare it with target

if (countTarget.join('') == anyString) {answer = true} else {answer = false} return answer;

if identical when true if not when false

Check your code with this and see what it is not doing what you think: http://pythontutor.com/javascript.html

Thank you for your help

Write again if you need other help!

I wrote all the code. I all remade

function confirmEnding(str, target) {
if (str.slice(-target.length) == target){
  return true;
} else {return false;
} }
confirmEnding("Bastian", "n"); 

Thanks so much for your advice.

I wanted to add this reply to the solutions page because it seems more concise than what is solutions are on the solutions page. However, I don’t know how to reply to the solutions page.

Here’s my solution:
function confirmEnding(str, target) {
return str.slice(-target.length) == target;
}

confirmEnding(“Bastian”, “n”);

I hope you all like this approach.

To make it even smaller, I turned it into an arrow function:
let confirmEnding = (str, target) => str.slice(-target.length) == target;

confirmEnding(“Bastian”, “n”);