-
-
Save AliceWonderland/0940eafd720a417af8465724afea0a8d to your computer and use it in GitHub Desktop.
8.1 Clone Machine created by smillaraaq - https://repl.it/HJMy/8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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