Why wont my code work? (post again since can't find post)

function confirmEnding(str, target) {  
  var stringLength = str.length;
  var arrayString = str.split('');  
  if(arrayString[str.length - 1] === target){
    truth = true;
    return truth;
  } else {
    truth = false;
    return truth;
return truth;

confirmEnding("Bastian", "n");

Please explain to me why my answer will not work.

For some reason, the “alert()” method no longer works with FreeCodeCamp and
console.log() has never worked for me, but I have no problem outside of FreeCodeCamp.
With the recent website changes, it seems I can no longer test variable values.

Sorry if this doesn’t show correctly. I have done this in the correct format so it separates out
the code. Again, my apologies for this being hard to read. something is broken on the new FreeCodeCamp website. :frowning:

Your browser information:

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

Challenge: Confirm the Ending

Link to the challenge:

Some of the tests are failing because Free Code Camp tests run in “strict mode”. This means must declare all variables used. In general, this is also considered a best practice.

Once you declare all used variables, then you still have some logic issues to figure out. Mainly, your if statement condition (see below):

arrayString[str.length - 1] === target

What if target is more than a single letter? arrayString[str.length -1] is only a single letter, but target could be 1 or more letters.

1 Like

Are you declaring the variable truth anywhere?

Thank you for your input.
I see now that my “truth” variable had not been defined.

I think in the answer to the section, they might not have defined “truth” either
as a variable. It has been a while since I looked at this.

Thanks for the help!