Skip to content

Instantly share code, notes, and snippets.

@AliceWonderland
Forked from anonymous/8.1 Clone Machine.js
Created April 18, 2017 07:28
Show Gist options
  • Save AliceWonderland/0940eafd720a417af8465724afea0a8d to your computer and use it in GitHub Desktop.
Save AliceWonderland/0940eafd720a417af8465724afea0a8d to your computer and use it in GitHub Desktop.
8.1 Clone Machine created by smillaraaq - https://repl.it/HJMy/8
var dolly = ["Dolly", "sheep", []];
var dollyClone = cloneAnimal(dolly);
var dollyClone2 = cloneAnimal(dolly);
var dollyClone3 = cloneAnimal(dolly);
// The clone is of same species, with new name and no offspring
console.log(dollyClone)
console.log(dollyClone2)// ["DollyClone", "sheep", []]
console.log(dollyClone3)
// The parent animal now has an offspring in its array
console.log(dolly) // ["Dolly", "sheep", ["DollyClone"]]
function cloneAnimal(parentAnimal){
var numOffspring=parentAnimal[2].length;
var cloneAnimal=parentAnimal.slice(0,2);
cloneAnimal[0]=parentAnimal.slice(0,1)+"Clone"+(numOffspring+1);
cloneAnimal.push([]);
parentAnimal[2].push(cloneAnimal[0]);
return cloneAnimal;
}
/* SCHOOL SOLUTION
function cloneAnimal(animal) {
var cloneName = animal[0] + "Clone";
var clone = [cloneName, animal[1], []];
animal[2].push(cloneName);
return clone;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment