Mutations challenge - Can anyone see what I am doing wrong?

Tell us what’s happening:

I am passing all test cases instead of the first one. Any ideas please?

  **Your code so far**
function mutation(arr) {

  // arr[0]

  let   firstElement = arr[0].toLowerCase();

// arr[1]

    let splitArr = arr[1].toLowerCase().split("");

// Just so I could see what was going on.

  console.log(splitArr, arr[0]);

// Iterating through the test array(2nd element).

  for(let i = 0; i < splitArr.length; i++) {

    if(firstElement.indexOf(splitArr[i]) <= -1){

      return false;

    }

    else {

      return true;

    }

  }

      

  }

mutation(["hello", "hey"]);

function mutation(arr) {

// arr[0]
let   firstElement = arr[0].toLowerCase();

// arr[1]
  let splitArr = arr[1].toLowerCase().split("");

// Just so I could see what was going on.
console.log(splitArr, arr[0]);

// Iterating through the test array(2nd element).
for(let i = 0; i < splitArr.length; i++) {

  if(firstElement.indexOf(splitArr[i]) <= -1){
    return false;
  }
  else {
    return true;
  }
}




}


mutation(["hello", "hey"]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0.

Challenge: Mutations

Link to the challenge:

Remeber that as soon as a return statement executes, the function stops.

Your for loop always hits a return, which means that instead of looping, it only checks a single value.

2 Likes

Oh my god, that’s so obvious now you’ve pointed it out, thank you so much. I’ve been scratching my head staring at this for hours lol.

Sorry, quick question if you’ve got a sec please. You see when you write an if statement, I will often see

if(condition) {
doStuff;
return doStuff;
}
return true;

with no else statement. Is this just assumed, the else statement? Or does it work diferently to using an actual else statement?

Thank you so much for your help. Really appreciated.

If there is a return inside the if, that means it’s like a else because it will only execute when condition is false.

On the other hand

if(condition) {
    console.log('true');
}
console.log('bye')

If condition is true, then it will print both “true” and “bye”.

2 Likes

Thanks, I assumed it was something along those lines but wasn’t 100%
Just so I am clear,

if(condition) {
            console.log('true');
}
return console.log('condition failed');

// So the above is the equivilent of having the following correct?

if(condition) {
            console.log('true');
}
else {
           return console.log('condition failed');
}

// Or would it need to be inside the if statements {} like so:

if(condition) {
            console.log('true');

return console.log('condition failed');
}

thank you for all your help :smiley:

no, because in the first case if condition is true, both inside and outside of the if statement get executed, in the second case only the inside of the if statement

what behaves the same is when you have a return statement inside the if statement., as if it executes the code stop there

The return is important here. A return exits the function, so nothing happens after it. If your if block does not have a return, then using an else makes a difference.

(You cannot meaningfully return a console statement. It will return undefined.)

If condition is truthy then it will print “true” AND hit the return. If condition is falsey, it will only hit the return.

if condition is truthy, it will print “true” and not hit the return. If it’s falsey, it will hit the return.

If condition is truthy it will print “truthy” AND hit the return. If it is falsey, it will do neither.

You can learn a lot by playing around with this sort of thing in your console, a node environement, or a site like repl.it.

1 Like

Cheers, I’m gonna have a little play around on this site you’ve suggested, I feel like an idiot, I am looking at it and I feel like my subconscious understands but I don’t lol.

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