 # freeCodeCamp Challenge Guide: Mutations

``````function mutation(arr) {
var flower = arr.toUpperCase();
var slower = arr.toUpperCase().split('');
return slower.every((c,i) => {
return flower.indexOf(c) !== -1;
});

}

mutation(["hello", "Hello"]);``````

This was so hard for me, don’t know why, the test[i] part I couln’t put in code, anyway this is my solution:

``````function mutation(arr) {
let test = arr.toLowerCase();
let target = arr.toLowerCase();

for (let i = 0; i < test.length; i++) {
if (!~target.indexOf(test[i]))
return false;
}
return true;

}

mutation(["Alien", "line"]);``````

can someone tell me what’s wrong with my code?? I don’t know why it’s not working for the first mutation but is working for the rest.

`````` function mutation(arr) {
var array = arr.join(',').toLowerCase().split(",");

var compare = array.split("");

for (var i = 0; i < array.length; i++) {
if (array.indexOf(compare[i]) === -1) {
return false;
}

else {
return true;
}

}

}

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

Hi campers . this is my code. Its not working with all tests, I could use some help to spot that bug.

function mutation(arr) {

for(j=0;j<arr.length;j++){
arr[j].toLowerCase();
b = [];
b = arr.split(’’);
for(i=0;i<b.length;i++){

``````result = arr.indexOf(b[i]);
if (result !== 0){
return true;
}else{
return false;
}
``````

}
}
return arr;
}

I split arr into letters, creating oneArr, then I used a while loop to add 1 to n as long as the letters from oneArr were present in arr. So if the loop stops executing when n < oneArr.length ,it means one of the letters isn’t present and the function will return ‘false’.

function mutation(arr) {

var oneArr = arr.toLowerCase().split("");
var n=0;
while ((arr.toLowerCase().indexOf(oneArr[n]) > -1) && (n < oneArr.length)) {
n++;
}
if ((n < oneArr.length) === true) {
return false;
}
return true;
}

1 Like

Babu, creating that empty b variable isn’t necessary if you declare it in the next line. Still, you need to use var before introducing a new variable so that should go before b = arr.split(’’). I can’t imagine you’d have to use a double for loop for this task. Also, your final condition result !== 0 isn’t the right condition here. Read again about indexOf() and what the values it returns mean. Read the tips in the head of this thread, they helped me a lot.

[quote=“samnguyen94, post:69, topic:16025”]
var array = arr.join(’,’).toLowerCase().split(",");
[/quote] - I believe this line is unnecessary. You could just split arr into two simply indexing arr and arr and then change them toLowerCase. You use the name ‘array’ instead of ‘arr’ all throughout your code. ‘array’ is never declared here

Thanks , I did some reading, used var to declare an empty array, didn’t see the need to split but definitely lower cased my strings. This is what I came up with.

function mutation(arr) {
var newArray = [];
for (i=0;i<arr.length;i++){

``````newArray.push(arr[i].toLowerCase());
``````

}for(j=0;j<newArray.length;j++){
if (newArray.indexOf(newArray[j]) === -1){
return false;
}

}
return true;}.

``````function mutation(arr) {
let string1 = arr.toLowerCase();
let string2 = arr.toLowerCase();

let letterInStr = true;
let i = 0;

while (i < string2.length && letterInStr === true) {
letterInStr = string1.includes(string2[i])
i = i + 1;
}

return letterInStr;
}

let result = mutation(["Mary", "Aarmy"]);
console.log(result);
``````