Solving Mutations algorithm

This is my solution, and it looks to me like a pretty man-baby-type-solution which is what I usually end up doing:

function mutation(arr) {
  arr = arr.map(function (elements) {
    return elements.toLowerCase();
  });
  
  let letters = arr[1].split('');
  
  let flagArray = [];
  let flag = 0;
  let i;
  let j;

  for (i = 0; i < letters.length; i++) {
    flagArray.push(0);
    for (j = 0; j < arr[0].length; j++) {
      if (letters[i] == arr[0][j]) {
        flag = 1;
        flagArray[i] = flag;
        break;
      }
    }
  }
  
  let notAllPresent = 0;

  for (let k = 0; k < flagArray.length; k++) {
    if (flagArray[k] == 0) {
      notAllPresent = 1;
      break;
    }
  }

  if (notAllPresent) {
    return false;
  } else {
    return true;
  }
}

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

Any shorter way to do this? By maybe using more built-in magics?

EDIT: Not sure of the category I’ve posted this in. Pardon me for that and feel free to transfer this into JavaScript category maybe…

:rofl: I feel like that all the time. Check out the solutions at the bottom of the “hints” article to glean some inspiration for more concise approaches: freeCodeCamp Challenge Guide: Mutations

1 Like

Here’s another one absent in hints for your inspiration :slight_smile:

const mutation = ([a, b]) => new RegExp(`^[${a}]+$`, 'i').test(b);
4 Likes

This is a oneliner beauty! Thanks for sharing! Solving problems in C for years still makes me produce long code like this! :smile:

I still get amazed how much readability and ease of coding languages like Python and JS provides, with their built-in magics.

Wow, just wow. I wished I could do that.