.toLowerCase help

Hello,

I am working on Mutations Algorithm and wondering how to lower case a element within an array if it is a string.

eg

``
function mutation(arr) {

var lowerCaseArray = arr[0].toLowerCase();
var lowerCaseArray1 = arr[1].toLowerCase();

for (i = 0; i < arr[1].length; i++) {

var nextArray = lowerCaseArray[0].indexOf(lowerCaseArray1[1]);
if (nextArray < 0) {
  return false;
}

}
return true;
}

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

I am wondering why this doesn;t work with my toLowerCase when it is a string in an array?

var lowerCaseArray = arr[0].toLowerCase();
var lowerCaseArray1 = arr[1].toLowerCase();

Right off the bat there is a problem in your naming -

var lowerCaseArray = arr[0].toLowerCase();
var lowerCaseArray1 = arr[1].toLowerCase();

Those aren’t arrays. arr is an array, but arr[0] is a string.

I found it easier to work with an array so I used split to break those strings into arrays, var lowerCaseArray = arr[0].split("");

I would also be worried about the line:

var nextArray = lowerCaseArray[0].indexOf(lowerCaseArray1[1]);

Did you mean i for an index instead of 1?

If that doesn’t help, come back and check with us.

First off, you should be looping through the LOWER CASE version of the string, not the original. This line loops through the original:

for (i = 0; i < arr[1].length; i++) {

instead, you should use:

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

Second, you are looping through the second string correctly, but then you keep checking against the same character in the string:

var nextArray = lowerCaseArray[0].indexOf(lowerCaseArray1[1]);

Notice, you hard coded 0 and 1, instead you should be checking using i:

var nextArray = lowerCaseArray.indexOf(lowerCaseArray1[i]);