Algorithm Challenge: Mutations feedback request (spoilers)

This problem took me some time, and I’m curious if my solution is inelegant/inefficient. I would welcome any feedback, or suggestions for improvement.

function mutation(arr) {
  var a = arr[1].toLowerCase().split('');
  var b = arr[0].toLowerCase();
  var i = 0;
  while ((b.indexOf(a[i]) >= 0) && (i < a.length)) {
  if (i === arr[1].length) {
    return true;
  } else return false;

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

You can replace the if-else block with this line:

return i === arr[1].length;

You can also replace (b.indexOf(a[i]) >= 0) with the more descriptive


I think you should have named your variables with names like first / second (or at least assign the first array to a).

I like the suggestion by @kevcomedia about using the includes function instead of the indexOf function and I like looping through a (in your example) with a for…loop and immediately return false if b does not include a letter in a. That way, when the loop is over, you know everything matched…

for (i=0;i<a.length;i++) {
  if (!b.includes(a[i])) return false;
return true;

Thank you for the simplifying suggestions - as regards the includes() method, I was avoiding using it because the prompt refers to the indexOf() method, and I don’t think includes() is described anywhere in the course work up to this point.

You are correct about includes not being including in the tutorial yet.