Mutations-Code Feedback for alternative solution

Mutations-Code Feedback for alternative solution
0.0 0

#1

Tell us what’s happening:

Hi there,
I’m still struggling to bring the given solution using ‘===-1’ and the advice of moz-dev
together, where ‘!==-1’ is the way to go to check if a string exists.
I tried both solutions, but only the first one works.
The second one using moz-dev-solution doesn’t work for the first example.
Why that?
Any help is appreciated.
Thanks in advance.
Best,
Jan

Your code so far

function mutation(arr) {
  var source= arr[0].toLowerCase();
  var target= arr[1].toLowerCase();
  //console.log(arr[1].toLowerCase().indexOf(arr[0].toLowerCase(),0));
  for(var i=0;i<target.length;i++){
    return(source.indexOf(target[i])!==-1);
    }
}

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

Your browser information:

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

Link to the challenge:


#2

indexOf will only return -1 if the given string is not found in the target. !== -1 is the only way to check if something exists as === -1 asserts that it does not exist.


#3

Hi PortableStick,
thanks for your quick reply.
Ok, so the description here is basically just wrong?
image


#4

No, it’s exactly right. Pay close attention to the string values being used.


#5

:smiley: Ok, I think i got your point now. Thanks for your help.
So if I still want to use my first solution this modification will do the job:

if((source.indexOf(target[i])!==-1)===false)
      return false; 

Cheers,
Jan


#6

Well, there’s another reason why the solution you posted isn’t going to work and it has nothing to do with indexOf. When you return inside of a loop, it exits the function. So if you don’t have any if statements in your loop, it’s just going to exit out before i ever increments.

But why do this…

if((source.indexOf(target[i])!==-1)===false) return false

… when you could do this?

if(source.indexOf(target[i]) === -1) return false

Don’t make statements more complicated than they have to be!


#7

Ok, many thanks for the clarification.

I totally agree with what you are saying. It was just for the purpose of demonstrating and to show that I now understand why my code didn’t work earlier.

Cheers,
Jan


#8
  *******Mutations (my solution)********

function mutation(arr) {

var x = arr[0].toLowerCase().split(’’);

var y = arr[1].toLowerCase().split(’’);

return y.every(function(val){

return x.includes(val) ;

});
}