Mutations I need a help

Tell us what’s happening:

Your code so far


function mutation(arr) {
let text =  arr.slice(1);
let result;
for(let i=0; i < arr.length -1; i++){
    result = arr[i]; 
    if(text.indexOf(result,0)){
       console.log(result); 
       return true; 
   }
       return false;
   }
}

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

````Please I need Help, my code doesn’t work just when I clicked true is matched, but false doesn’t do anything.```

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/

Hello @osandoval1976,

Your code is not working because you are returning either true or false on the first iteration of the for loop. Try refactoring your code so that every letter of arr[1] is checked before returning a value.

Hint: consider splitting the words into arrays.

1 Like

Your for loop will only make one iteration, because you are returning either true or false based on the if statement condition. Remember, a function exits as soon as a return statement is executed.

1 Like

Ok, let how I resolver this. Thanks,

You will have to rethink your current algorithm. Write out your algorithm steps in plain language and take an example and work through the steps to validate your logic would work. Then (and only then) should you try to write out the code for the algorithm. If you have trouble writing out your algorithm, post what you have (even pseudo-code) and we can try and guide you.

1 Like

Thanks, I am new in coding some challenges for me are tricky.

``hey guys, I try my best, this is my last code.```

function mutation(arr) {
let result;
for(let i =0; i <= arr.length; i++){
    arr= arr[i].split("");
     if(arr[1].indexOf(arr[0]) && arr !==-1) {
         console.log(arr);
           return true;
     }
            return false;
     }
       
}   
mutation(["hello", "hey"]);```

You may want to see what happens in your code with a tool like this:
http://pythontutor.com/javascript.html

I see a few logic issues with the variables, and various issues overall

What are you trying to do with the if statement? I can’t understand, and there is no way that that would work

1 Like

I just try to solved the problem, I tried other codes but does not work.
I try to learn with no cheating.

Ok, lets make a step back. Forget about coding.

Try to explain in the smallest details how you would work trough this. Consider you have a word of 300 characters so you can’t just solve it at a glance, you need a method that will make sure you check all characters one by one.
Try to write the logic, the pseudo-code

1 Like