Mutations(help!)

When I move the return result statement inside the loop it fails only one test and when I move it out the loop it fails three. I believe the loop isn’t going till the end of the array and that is the reason I saved the answer to newArr11.includes(newArr22[i]) and returned the result outside the loop so it won’t exit out of it early. I also tried if/else statements in the loop with return true, else return false but that also fails one test. I feel like I have done everything right except one thing, can anyone give me some suggestions? Also I have posted about this challenge before but this is another question which I cannot ask in the same post. So I would appreciate it if the mods won’t get in the way, unless you want to make it clear in the Read-Search-Ask page & and every FCC user that you are only allowed one question per challenge. Thanks!


function mutation(arr) {
let result=0
let newArr1=arr[0]
let newArr2=arr[1]
let newArr11=newArr1.toString().toLowerCase().split("")
console.log(newArr11)
let newArr22=newArr2.toString().toLowerCase().split("")
console.log(newArr22)

for (let i=0;i<newArr22.length;i++) {
  result=newArr11.includes(newArr22[i])
}
  return result
}

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


Your browser information:

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

Challenge: Mutations

Link to the challenge:

well, strings are “array like objects”
and they have the includes method
so
try this …

function mutation(arr) {
  var ar0=arr[0].toLowerCase();
  var ar1=arr[1].toLowerCase();
  console.log(ar0.includes(ar1[0]));
  console.log(ar0.includes(ar1[1]));
  console.log(ar0.includes(ar1[2]));
}
mutation(["hello", "HEY"]);

hope this helps

1 Like

You have some wierd naming going on (strings are not arrays, and it’s easier on the brain if you name things for what they are). Also you don’t need to use toString on a string. And you don’t need to convert to an array: strings also have the includes method and can be looped over just fine. But anyway:

So mutation(["hello", "neo"]). If return is outside the loop:

n is not in hello: result is now false
e is in hello: result is now true
o is in hello: result is now true

return result, so return true

You’re just overwriting the value of result. So if the last letter in the second string is in in the first string, you always return true regardless if you should or not.

So in fact, this does exactly the same as your code:

function mutation([str1, str2]) {
  // Just check whether the last character in str2 is in str1:
  return str1.includes(str2[str2.length - 1]);
}

Whereas is if is false at all, you need to return false straightaway. With the example “hello” and “neo”, “n” is not in “hello”, so you must return false.

1 Like

Alright this is the if statement version of my mess-up

function mutation(arr) {
  
  let newArr1=arr[0]
  let newArr2=arr[1]
  let newArr11=newArr1.toString().toLowerCase().split("")
  console.log(newArr11)
  let newArr22=newArr2.toString().toLowerCase().split("")
  console.log(newArr22)
  
  for (let i=0;i<newArr22.length;i++) {
    if (newArr11.includes(newArr22[i])) {
      return true
    }
    return false
    
  }
}

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

I tried switching statements to check if it is false before true and the code you see here is checking if its true before false. No matter what I try the loop just fails to reach the end? It’s day 3 for me on this and I would hate to just give up on it.

Ah, so now, if the first letter of the second string is included in the first string, then the function returns true. So ["hello", "ola"] would return true. Your prevoius code got to the end of the loop fine – this one won’t because you’re returning whether it’s included or not. This works occasionally, which is why some tests pass.

To fix: if it’s false, exit immediately (if it doesn’t include the letter, then return false). Otherwise keep looping. If you get to the end, everything’s fine, you can return true outside the loop. At the minute, you have the logic inverted.

1 Like
function mutation(arr) {
  
  let newArr1=arr[0]
  let newArr2=arr[1]
  let newArr11=newArr1.toString().toLowerCase().split("")
  console.log(newArr11)
  let newArr22=newArr2.toString().toLowerCase().split("")
  console.log(newArr22)
  
  for (let i=0;i<newArr22.length;i++) {
    if (newArr11.includes(newArr22[i])==false) {
      return false
    }
    return true
    
  }
}

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

So my code doesn’t exit immediately? It’s still showing true for [hello,hey] and this is pretty much all I got. I’m finding this pretty hard and am questioning how I’m gonna be able to do anything else within JS. This is such a simple task, to compare arrays and it’s giving me utter hell lol

I appreciate the help! I had used what you had blurred out previously

if (newArr11.includes(newArr22[i])==false) {
      return false
    }
    return true

on multiple occasions but I never put the return true outside the loop. So I had got rid of checking whether it was false all together because it wasn’t passing. I guess if I had just returned outside the loop on those occasions I wouldn’t have even needed to ask, but I guess I was burning through the methods too quickly. Thanks for pointing out the “outside of the loop” idea. Positioning positioning positioning!

1 Like