Mutations longer syntax sollution not working

So I was wondering why does this code not work since the first solution looks the same to me except it’s written in a shorter syntax.

Actually, only the first test with hello and hey doesn’t work.


function mutation(arr) {

let test = arr[1].toLowerCase();
let target = arr[0].toLowerCase();

for(let i = 0; i < arr.length; i++){
  if(target.indexOf(test[i]) < 0){
    return false;
  }else {
    return true;
  }
}

}

console.log(mutation(["hello", "hey"]));

Challenge: Mutations

Link to the challenge:

Are you able to give the answer just checking the first letter of the string?

To build on ilenia’s response, remember that a return will exit you from the function.

Also, think about how long arr is, and whether or not that should be the number of iterations in your for loop.

I was thinking in that direction too, however isn’t that code(code from sollution 1) the same like mine but written in a shortened syntax?

function mutation(arr) {
  let test = arr[1].toLowerCase();
  let target = arr[0].toLowerCase();
  for (let i = 0; i < test.length; i++) {
    if (target.indexOf(test[i]) < 0) return false;
  }
  return true;
}

Edit: In lamens terms, what I’m trying to understand is what’s the difference between this code(what I’ve written):

function mutation(arr) {

let test = arr[1].toLowerCase();
let target = arr[0].toLowerCase();
  
    for(let i = 0; i < test.length; i++){
      if(target.indexOf(test[i]) < 0){
        return false;
      }else {
        return true;
      }
    }
  
}

And this code(which is the first solution in hints):

function mutation(arr) {
  let test = arr[1].toLowerCase();
  let target = arr[0].toLowerCase();
  for (let i = 0; i < test.length; i++) {
    if (target.indexOf(test[i]) < 0) return false;
  }
  return true;
}

You mean by: console loging the function with (h , h) only instead of (hello, hey)? If yes, then yes and it returns true.

Oooh, now I see it
Thanks a lot!!!

Oh no wait, I changed arr.length to test.length and it didn’t fix it somehow.

There is a big difference between what you have there and what you originally posted. More than simply shortened syntax, that is different logic.

Trance through the logic, step by step, using one of the failing tests and see if you can understand why one works and the other doesn’t. That is an important skill for a developer.

Sorry, you edited the post to which I was responding after I responded. But my comment still stands - there is a big difference between those two, in terms of logic/flow. Step through a simple example, like one of the ones that was failing and see if you can spot why. Keep in mind the comments of others that are trying to point you to the right direction.

1 Like

At the risk of giving too much away, it has to do with when and under what conditions this line is run:

return true;
1 Like

So yeah I saw my mistake, let me try to guess what might be happening here,
the function returns true by default in the solution while my function returns either false or true. Thanks for the help I apreciate it a lot!

2 Likes

Yeah, I think that’s right, but the phrasing I would add a little to it, I would say:

The working function returns true by default if the loop never returned found a mismatch and returned false while the non-working function returns either false or true on the first iteration of the loop and never gets past that first iteration (it only checks the first character.

I think that’s what you’re saying - I’m just picky about words and clarity.