Skip to content

Instantly share code, notes, and snippets.

Created April 18, 2017 07:28

Revisions

  1. @invalid-email-address Anonymous created this gist Apr 18, 2017.
    39 changes: 39 additions & 0 deletions 8.1 Clone Machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@


    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;
    }
    */