Can't seem to get this one

Hey guys, I’m not getting any false values so it’s not working. What am I doing wrong with the loop?

  **Your code so far**

function mutation(arr) {
var newStr0 = arr[0].toLowerCase();
var newStr1 = arr[1].toLowerCase()
var newArr0 = newStr0.split("");
var newArr1 = newStr1.split("");

  for (let j = 0; j < newArr1.length; j++) { // loop through all the letter in 1 index of arr
    if (newArr0.indexOf(newArr1[j]) >= 0); // if all letters in separated string of arr[0] has a index of equal to order greater than 0 then return true
  } return true;
} false;


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

// initialize newArr1 as arr[1] with separated string 
// initalize newArr0 as arr[0] with separated string
// loop through newArr0 to see at what index of newArr0 is newArr1
// if === -1
// return false
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36.

Challenge: Mutations

Link to the challenge:

1 Like

you may want to check that what you have written correspond to your logic

what is that false doing outside the function?

what is that semicolon after the if condition, without code to execute?

1 Like

The code you pasted above is visually cluttered. I know the FCC editor doesn’t format code by default so you have to add the indentations manually but I highly recommend you do that as I think it will help you see where your logic is going wrong.

Also, I’m not sure if you are adding those comments for our sake or yours, but too many comments can sometimes be just as bad as too few. If you do need comments then try to make them as concise as possible. I would also recommend you put them directly above the line you are commenting on instead of after.

I think all of the above is contributing to the fact that you have forgotten the curly braces for the if statement.

2 Likes

You’re very close but your logic is off. Like ieahleen said, there’s a rogue false hanging at the end.
Also, you can simplify the first few lines by chaining
.toLowerCase().split("")

1 Like

Ok I fixed the code, but the code is still not functioning properly to get correct results

function mutation(arr) {
  var newArr0 = arr[0].toLowerCase().split("");
  var newArr1 = arr[1].toLowerCase().split("");
    for (let j = 0; j < newArr1.length; j++) { 
      if (newArr0.indexOf(newArr1[j]) >= 0) {
     return true;
  } 
    }
}
console.log(mutation(["hello", "neo"]));
1 Like

so if the first letter checked is present, you don’t need to check the others, and can return true immediately?

1 Like

Ok so I should add a for loop and nest this within it?

1 Like

what I quoted is inside a loop

how many of the letters you need to check before you can be sure that the answer is true?

1 Like

All of them but honestly I can’t think of how to do that. I tried a few things but none of it are working

1 Like

it’s the same general idea of last challenge, things happen exactly when you write them

if you need to check all of them before being able to say true then it’s after that that you can do it

2 Likes

Oh I see, so it’s better to first find when it’s false unless all are checked and true. I wasn’t thinking about it in that way.

1 Like