Basic Algorithm Scripting - Confirm the Ending

I’m not sure why this code isn’t being accepted as correct. I’m not trying to dispute this isn’t the fastest or cleanest solution, but it should provide a correct answer to each one of the tests. I tried debugging it, even in VS Code, and it does return the expected answer.

function confirmEnding(str, target) {

  const newStr = str.split('').reverse();
  const newTarget = target.split('').reverse();
  const sameEnding = newTarget.reduce((acc, element, index) => element === newStr[index] ? acc += '' : acc = false, true);
  
  return sameEnding;


}

confirmEnding("Bastian", "n");

Your browser information:

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

Challenge: Basic Algorithm Scripting - Confirm the Ending

Link to the challenge:

Your result should not be a string

This forces your result to be a string

1 Like

Thank you. I didn’t realize I was changing the type from boolean to spring by adding an empty space.

I rewrote that line as it follows and, now, it works perfectly.

const sameEnding = newTarget.reduce((acc, element, index) => element === newStr[index] && acc, true);
1 Like

Nice work.

Since your reduce is now accumulating a boolean result, you could do that with a .every()

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.