Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2016 19:43
Show Gist options
  • Save anonymous/4667ee72ad330691540025f41910cb36 to your computer and use it in GitHub Desktop.
Save anonymous/4667ee72ad330691540025f41910cb36 to your computer and use it in GitHub Desktop.
https://repl.it/CQC7/261 created by sethopia
//Animal constructor function
function Animal(species, call) {
this.species = species;
this.call = call;
this.speak = function (call) {
console.log("The " + this.species + " says " + this.call)
}
}
//The Animals. Follow the design pattern to make as many as you want and add them to the zoo
var horse = new Animal('horse', 'neigh');
var wolf = new Animal('wolf', 'growl');
var squirrel = new Animal('squirrel', 'chip chip');
var cow = new Animal('cow', 'moo');
var narwhal = new Animal('narwhal', 'blub blub');
var okapi = new Animal('okapi', '???');
//Zoo object with nested exhibit objects
var zoo = {
"exhibits" : {
"Farm" : {
"animals" : [cow, horse],
"open" : true
},
"Grasslands" : {
"animals" : [okapi],
"open" : true
},
"Forest" : {
"animals" : [wolf, squirrel],
"open" : true
},
"Arctic" : {
"animals" : [narwhal],
"open" : false
}
}
};
//Complete this function that will take an exhibit string as an input
// The funcion should "visit" the given exhibit (if it is open) and log out each of the animals "speaking". If the exhibit isn't open, the function should log a message telling the visitor to try another one
function visit(exhibit) {
if (!zoo.exhibits[exhibit].open) {
console.log("Sorry, the " + exhibit + " exhibit is closed.");
} else {
var exhibitAnimalsArr = zoo.exhibits[exhibit].animals;
for (var i = 0; i < exhibitAnimalsArr.length; i++) {
exhibitAnimalsArr[i].speak();
}
}
}
visit("Arctic");
visit("Forest");
Native Browser JavaScript
>>> Sorry, the Arctic exhibit is closed.
The wolf says growl
The squirrel says chip chip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment