Basic Algorithm Scripting: Mutations (neat)

Hi, here is a neat solution to mutation problem using the every() and includes() methods. After ‘lower casing’ then splitting the strings into individual character elements of arrays…You saw it here first :slight_smile:

function mutation(arr) {
let arr0 = arr[0].toLowerCase().split("");
let arr1 = arr[1].toLowerCase().split("");
// For every element in first array is it included in second array?
return arr1.every( (elem) => arr0.includes(elem) );

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

Nice and neat, short and sweet.

1 Like

and converted to ES6.

you only need to split the second array to check for every element. the first array doesn’t need to be split because includes function will loop over the entire string in arr[0].

const mutation = (arr) => {
  return (arr[1].toLowerCase().split('').every((elem) => arr[0].toLowerCase().includes(elem)));

Yes, even neater :grinning:

This is a nitpick, so I’m sorry in advance: this pops up fairly often on the forums and it just happens that yours was the one that triggered this response:


const mutation = (arr) => {

Is not “the ES6 version” of

function mutation (arr) {

Saying that changing the second to the first is like saying this:

var mutation = function (arr) {

Is “almost ES6”