Basic algorithm scripting: Mutations - help needed

Basic algorithm scripting: Mutations - help needed
0

#1

I think I may have been looking at this for too long…would appreicate it if someone could tell me where I’m going wrong here…

function mutation(arr) {
let mainString = arr[0].toLowerCase();
let testLetters = arr[1].toLowerCase();
console.log(mainString);
console.log(testLetters);

for(let i = 0; i > mainString.length; i++) {
if(mainString.indexOf(testLetters[i]) < 0) {
return false;
}
}
return true;
}

mutation([“hello”, “hey”]);


#2

Can you format your code and add a link to the exercise?


#3
function mutation(arr) {
let mainString = arr[0].toLowerCase();
let testLetters = arr[1].toLowerCase();
console.log(mainString);
console.log(testLetters);

for(let i = 0; i > mainString.length; i++) {
if(mainString.indexOf(testLetters[i]) < 0) {
return false;
}
}
return true;
}

mutation([“hello”, “hey”]);

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

Apologies - here you go. Thanks!


#4

One error I see is here.

i > mainString.length doesn’t make sense because 0 can’t be greater than mainString.length, therefore your forloop will never execute.


#5

So simple! Thanks @shimphillip!

I have a bit of a blind spot with greater than and less than - I usually try both just to check. Can’t believe I didn’t here!

Thanks again - appreicate your help.


#6

Hi, what if testLetters is longer than mainString?
Perhaps you should replace mainString.length with testLetters.length