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);
6 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.

I’ve just been experimenting with this one and got stuck for a while, but eventually solved it with some quite simple code by leaning on the functional array features:

function mutation(arr) {
  let target = [...arr[0].toLowerCase()];
  let source = [...arr[1].toLowerCase()];

  return source.every(item => (target.indexOf(item) > -1));
}

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

Use the [...arr[0]] spread operator to get split the string into an array, then every to ensure that all must match for it to pass.

But the difference between the two are two different approaches, so its not like one is better or worse. I’ve written it in a functional way and you’ve used an imperative approach:

I did the imperative patterns for a long time, and now im relearning / rewiring myself to try to think in more functional terms.

Edit: Although, looking at yours , there are a few spots for improvement, like you can just use a bool directly here:

  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;
  }

could be

  let notAllPresent = false;

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

  return notAllPresent;

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.