Review: Mutations Challenge

Review: Mutations Challenge
0

#1

Hello Friends
I’ve just completed my Mutations Challenge and want you all to please review it and let me know if it needs some improvements.

Many thanks in advance. Here’s the code:

function mutation(arr) {
  arr[0] = arr[0].toLowerCase();
  arr[1] = arr[1].toLowerCase();
  var myArr = arr[1].split('');
  
  for (var i = 0; i < myArr.length; i++) {
    if (arr[0].indexOf(myArr[i]) < 0) {
      return false;
    }
  }
  return true;
}

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

Happy Coding! :smile:


#2

If you are not wanting to assign the lowercase versions of arr[0] and arr[1] to variables names, then you could just go with the following and skip the creation of the myArr variable. Strings also have an indexOf function. I am not recommending the following code, because it does not read as well as having variable names for the two elements in arr.

function mutation(arr) {
  arr[0] = arr[0].toLowerCase();
  arr[1] = arr[1].toLowerCase();
  for (var i = 0; i < arr[1].length; i++) {
    if (arr[0].indexOf(arr[1][i]) < 0) {
      return false;
    }
  }
  return true;
}

#3

Thank you for your reply. Adding lowercase versions are the part of challenge, that’s why I’ve added them. Here is the challenge:

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
Remember to use Read-Search-Ask if you get stuck. Write your own code.

#4

For better reading you can visit the link: https://www.freecodecamp.com/challenges/mutations


#5

I know the challenge. I did not say anything about not using the lowercase function. I only said you could remove the following line in your posted code and use the arr[1] in your code instead of myArr.

var myArr = arr[1].split('');

#6

May be i was not clear, I just need opinion to do it with best practice. If i skip the lowercase then it will not show me h == H. That’s why looking for help. What will be the best practice if i skip the lowercase, then how can i evaluate the lower & upper case?

Hope you understand my problem here.


#7

If you do not want to use toLowerCase, then you could use a regular expression as follows:

function mutation(arr) {
  var re = new RegExp('[^'+arr[0]+']', "i");
  return !re.test(arr[1]);
}