Basic Algorithm Scripting: Confirm the Ending - where's the bug

can someone tell me why my code isn’t working?? can’t fix the bug!!

function confirmEnding(str, target) {
  let a = target.length - 1;
  let b = str.length - 1 - a;
  let car = str[b];
  for(let i = b; i < str.length; i++){
    car = car + str[i];
  if(car === target){
    return true;
  } else {
    return false;


I’m not sure if this is the solution, but try surrounding str.length - 1 with parenthesis ( ). You’re letting the compiler decide what operation to do first based on its operator precedence list.

So here is a very useful tip:

When you have an expression like that with two operator of the same type, surround with parenthesis which one you want to perform first. It will look much easier for you and for other people to understand your code.

that wasn’t the bug but thanks for the tip man

1 Like

Take a closer look at car variable before you start the loop and after it ends. You can use console.log(car); to display variable value in the console. This should point you in the right direction.

If you’d need another tip after that, here’s one, a little more explicit, in the spoiler:

Are you sure your loop starts at the appropriate index?