Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/45c31999408221d9cac7 to your computer and use it in GitHub Desktop.
Save anonymous/45c31999408221d9cac7 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/elliotfriend 's solution for Bonfire: Mutations
// Bonfire: Mutations
// Author: @elliotfriend
// Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations?solution=function%20mutation(arr)%20%7B%0A%20%20var%20indexLocation%20%3D%200%3B%0A%20%20var%20targetArray%20%3D%20arr%5B0%5D.toLowerCase().split(%22%22)%3B%0A%20%20var%20goalArray%20%3D%20arr%5B1%5D.toLowerCase().split(%22%22)%3B%0A%20%20while%20(goalArray.length%20%3E%200)%20%7B%0A%20%20%20%20indexLocation%20%3D%20targetArray.indexOf(goalArray%5B0%5D)%3B%0A%20%20%20%20if%20(indexLocation%20!%3D%3D%20-1)%20%7B%0A%20%20%20%20%20%20goalArray.shift()%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20return%20true%3B%0A%7D%0A%0Amutation(%5B%22hello%22%2C%20%22mhey%22%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function mutation(arr) {
var indexLocation = 0;
var targetArray = arr[0].toLowerCase().split("");
var goalArray = arr[1].toLowerCase().split("");
while (goalArray.length > 0) {
indexLocation = targetArray.indexOf(goalArray[0]);
if (indexLocation !== -1) {
goalArray.shift();
} else {
return false;
}
}
return true;
}
mutation(["hello", "mhey"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment