Mutations(Spoiler: Solution)

@P1xt I think it’s possible to use Regular Expressions with a similar approach to the “did any of these not match”

function mutation(arr) {
  for (var i = 0; i < arr[1].length; i++){
    var pattern = arr[1][i];
    var re = new RegExp(pattern, "gi");
    if (!re.test(arr[0]))
      return false;
  }
 
  return true;
}

Although this solution looks very clumsy, it was very important and useful for me to solve this problem by myself and gain rich experience on how each method works (and how my brain works too). After I solved this problem I’ve found in this topic the best solution and study it carefully. I try to avoid all unnecessary steps in my code in future. Thanks to everybody in this topic!!!

function mutation(arr) {

var arr1 = arr.slice(0,1);
var arr2 = arr.splice(1);

var arrToString1 = arr1.toString(’’);
var arrToString2 = arr2.toString(’’);

arrToString1=arrToString1.toLowerCase();
arrToString2=arrToString2.toLowerCase();

arrToString1=arrToString1.split(’’);
arrToString2=arrToString2.split(’’);

arrToString1=arrToString1.sort(’’);
arrToString2=arrToString2.sort(’’);

var arr33 = arrToString1.join(’’);
var arr44 = arrToString2.join(’’);

for (var i=0; i<arr44.length; i++) {
if ( arr33.indexOf(arr44[i]) === -1)
{return false;}}

return true;}

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

Lot of code there guys…
My solution is simple.

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

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

Good try :thumbsup: I do not recommend your approach though.

A better solution:

function mutation(arr) {
	// Iterate through every character in the second string, to check if it is present in the first string
	for (var i=0; i<arr[1].length; i++) {
                // Convert all characters to lower case
		if ( arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) == -1 ) {
			return false;
		}
	}
	return true;
}

mutation(["Mary", "Army"]);

I think the problem is your else statement is in for loop

Some really clean solutions!

Here is my, less clean, solution:


function mutation(arr) {
  var x = arr[0].toLowerCase();
  var y = arr[1].toLowerCase().split("");
  var matches = 0;
  for (var i = 0; i < y.length; i++){
  if (x.match(y[i])){
    matches++;
    }
  }
if (matches === y.length){
  return true;
}
  else return false;
}

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

There are some beautiful and concise answers! :astonished: :thumbsup:

Here’s my rather convoluted solution — but with only things learned from earlier exercices. Does it make any sense doing this ?

function mutation(arr) {
  // Preparing the array so words like "Hello" become "helo",
  // and "VoOdoO" become "vodo".
  // in order to get unique letters, and get rid of duplicates
  var preparedArray = arr.map(function(val){
    return val
            .toLowerCase()
             .split('') // to process individual letters
              .reduce(function(result, current){
                 var len = result.length;
                 // rebuild the word with only unique letters
                 // if the current letter is already in our string, pass
                 // else append it to the word.
                 while (len){
                   if (result[len - 1] === current)
                     return result;
                   len--;
                 }
                 return result + current;
             }, "");
  });
  
  // Get minimum required letters by getting
  // the length of the shortest word in the array
  var letters = Math.min(preparedArray[0].length, preparedArray[1].length);
  
  // Comparing the letters in both words,
  // decrementing the minimum letter requirement
  for (var i = 0; i < preparedArray[0].length; i++){
    for (var j = 0; j < preparedArray[1].length; j++){
      if (preparedArray[0][i] === preparedArray[1][j])
        letters--;
    }
  }
  // If the minimum is reached, return "true"
  return letters === 0;
}

Who r u? how u practice?How u write this better code?I am new here and also new to coding ,that is why i am asking.My codes r not this clear and efficient as Yours.do u practice day and night.

Utilizing Array.prototype.every()

function mutation(arr) {
  var a = arr[0].toLowerCase().split('');
  var b = arr[1].toLowerCase().split('');
  return b.every(function (element) {
    return a.indexOf(element) > -1;
  });
}

Maybe I’m a bit more experienced than you :slight_smile:
I think first time my code was awful but 10 years of practice… and practice… and practice… (I’m java-dev) and now it’s much better I hope.
So, just learn and practice and you’ll manage, belive me.

Please put in ur comments :slight_smile: 

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

@rmdawson71 : Thanks :slight_smile: Will do !

My solution

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

open for comments

This is what I have written:

function mutation(arr) {

var firstElement = arr[0].toLowerCase().split("");
var secondElement = arr[1].toLowerCase().split("");

for (i = 0; i < secondElement.length; i++) {
if (firstElement.indexOf(secondElement[i]) == -1) {
return false;
}
}
return true;
}

mutation([“khellkO”, “kfh”]);

here’s mine:

const mutation = ([orig, mutant]) =>
  ((o, ms) => ms.every(m => o.includes(m)))
  (orig.toLowerCase(), mutant.toLowerCase().split(''))