Skip to content

Instantly share code, notes, and snippets.

@cnocon
Forked from dbc-challenges/zoo.js
Last active August 29, 2015 13:56
Show Gist options
  • Save cnocon/8844953 to your computer and use it in GitHub Desktop.
Save cnocon/8844953 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
function Zoo(animals) {
this.animals = animals;
function(animals) {
var bipedsArr = new Array;
for (var i=0; i < animals.length; i++) {
if (animals[i].num_legs == 2) {
bipedsArr.push(animals[i]);
}
}
this.bipeds = bipedsArr
var quadrupedsArr = new Array;
for (var i=0; i < animals.length; i++) {
if (animals[i].num_legs == 4) {
quadrupedsArr.push(animals[i]);
}
}
this.quadrupeds = quadrupedsArr;
}
} // end init
var animals = [
new Animal("Human", 2),
new Animal("Monkey", 2),
new Animal("Kangaroo", 2),
new Animal("Horse", 4),
new Animal("Cow", 4),
new Animal("Centipede", 100)
];
function Animal(name, num_legs) {
this.name = name;
this.num_legs = num_legs;
this.identify = function() {
// console.log("I am a " + this.name + " with " + this.num_legs " legs. , " + this.name + "s have " + this.num_legs + " legs.");
}
} // end Animal
// function Zoo(animals) {
// this.animals = animals;
// this.init = function(animals) {
// this.animals = animals;
// for (var i=0; i < animals.length; i++) {
// new Animal(animals[i][0], animals[i][1])
// }
// }
// //
// } // end zoo
// bipeds = [];
// quadrupeds = [];
// init : function(animals) {
// for (var i=0, i < animals.length, i++) {
// if (animals[i].num_legs == 4) {
// quadrupeds.push(animals[i];
// } elsif { (animals[i].num_legs == 2)
// bipeds.push(animals[i]);
// }
// }
// }
</script>
<!--script type="text/javascript" src="zoo.js"/></script-->
</head>
<body>
</body>
</html>
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
// function Zoo(animals) {
// this.animals = animals;
// function(animals) {
// var bipedsArr = new Array;
// for (var i=0; i < animals.length; i++) {
// if (animals[i].num_legs == 2) {
// bipedsArr.push(animals[i]);
// }
// }
// this.bipeds = bipedsArr
// var quadrupedsArr = new Array;
// for (var i=0; i < animals.length; i++) {
// if (animals[i].num_legs == 4) {
// quadrupedsArr.push(animals[i]);
// }
// }
// this.quadrupeds = quadrupedsArr;
// }
// } // end init
function Animal(name, num_legs) {
this.name = name;
this.num_legs = num_legs;
// this.identify = function() {
// console.log("I am a " + this.name + " with " + this.num_legs " legs. , " + this.name + "s have " + this.num_legs + " legs.");
// }
} // end Animal
Animal.prototype.identify = function() {
var a = "I am a " + this.name + " with " + this.num_legs " legs. , " + this.name + "s have " + this.num_legs + " legs.")
console.log(this);
}
Zoo = {
init: function(animals){
this.animals = animals
},
bipeds: function(){
return this.animals.filter(function(animal){
return animal.num_legs === 2;
});
}
}
// function Zoo(animals) {
// this.animals = animals;
// this.init = function(animals) {
// this.animals = animals;
// for (var i=0; i < animals.length; i++) {
// new Animal(animals[i][0], animals[i][1])
// }
// }
// //
// } // end zoo
// bipeds = [];
// quadrupeds = [];
// init : function(animals) {
// for (var i=0, i < animals.length, i++) {
// if (animals[i].num_legs == 4) {
// quadrupeds.push(animals[i];
// } elsif { (animals[i].num_legs == 2)
// bipeds.push(animals[i]);
// }
// }
// }
function assert(test, message) {
if (!test) {
throw "ERROR: " + message;
}
return true;
}
var animals = [
new Animal("Human", 2),
new Animal("Monkey", 2),
new Animal("Kangaroo", 2),
new Animal("Horse", 4),
new Animal("Cow", 4),
new Animal("Centipede", 100)
];
// Zoo.init(animals);
// assert(
// Zoo.bipeds().length === 3, "the Zoo should have 3 bipeds"
// );
// assert(
// Zoo.quadrupeds().length === 2, "the Zoo should have 2 bipeds"
// );
// assert(
// animals[0].identify() === "I am a Human with 2 legs.", "humans have 2 legs"
// );
// assert(
// animals[2].name === "Kangaroo", "expected 'Kangaroo'"
// );
assert(
// console.log(animals[0].identify)
animals[0].identify === animals[5].identify, "only one implementation of the identify() function should exist"
);
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
function Zoo(animals) {
this.animals = animals;
this.init = function(animals) {
this.animals = animals;
for (var i=0, i < animals.length, i++) {
new Animal(animals[i][0], animals[i][1])
}
}
// this.bipeds = function(animals) {
// var bipeds = []
// for (var i=0, i < animals.length, i++) {
// if { (animals[i].num_legs == 2)
// bipeds.push(animals[i]);
// }
// }
// return bipeds;
// }
}
function Animal(name, num_legs) {
this.name = name;
this.num_legs = num_legs;
this.identify = function() {
'I am a ' + this.name + ' with ' + this.num_legs ' legs. , ' + this.name + 's have ' + this.num_legs + ' legs.'
}
function animals() {
Zoo.call(this)
}
}
// bipeds = [];
// quadrupeds = [];
// init : function(animals) {
// for (var i=0, i < animals.length, i++) {
// if (animals[i].num_legs == 4) {
// quadrupeds.push(animals[i];
// } elsif { (animals[i].num_legs == 2)
// bipeds.push(animals[i]);
// }
// }
// }
}
//------------------------------------------------------------------------------------------------------------------
// DRIVER CODE: Do **NOT** change anything below this point. Your task is to implement code above to make this work.
//------------------------------------------------------------------------------------------------------------------
function assert(test, message) {
if (!test) {
throw "ERROR: " + message;
}
return true;
}
var animals = [
new Animal("Human", 2),
new Animal("Monkey", 2),
new Animal("Kangaroo", 2),
new Animal("Horse", 4),
new Animal("Cow", 4),
new Animal("Centipede", 100)
];
//Zoo.init(animals);
z = new Zoo(animals)
console.log(z)
assert(
Zoo.bipeds().length === 3, "the Zoo should have 3 bipeds"
);
assert(
Zoo.quadrupeds().length === 2, "the Zoo should have 2 bipeds"
);
assert(
animals[0].identify() === "I am a Human with 2 legs.", "humans have 2 legs"
);
assert(
animals[2].name === "Kangaroo", "expected 'Kangaroo'"
);
assert(
animals[0].identify === animals[5].identify, "only one implementation of the identify() function should exist"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment