Skip to content

Instantly share code, notes, and snippets.

@bessiec
Created July 8, 2013 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bessiec/5952997 to your computer and use it in GitHub Desktop.
Save bessiec/5952997 to your computer and use it in GitHub Desktop.
var tacos = {
title:"Yummy Tacos",
servings: 6,
ingredients:["cilantro", "tortillas", "guacamole", "salsa", "cheese"]
}
function printRecipe(recipe){
console.log(recipe.title);
console.log("serves " + recipe.servings);
for (var i=0; i<recipe.ingredients.length; i++){
console.log(recipe.ingredients[i]);
}
}
printRecipe(tacos);
var pumpkinpie = {
title:"Delish Pumpkin Pie",
servings: 8,
ingredients:["crust", "pumpkin", "whipped cream"]
}
var recipes = [tacos, pumpkinpie]
for (var i=0; i<recipes.length; i++){
printRecipe(recipes[i]);
}
var books = [{
title: "Watership Down",
author: "Richard Adams",
alreadyRead: true
},
{
title: "James and the Giant Peach",
author: "Ronald Dahl",
alreadyRead: false
},
{
title: "50 Shades of Grey",
author: "Some lady",
alreadyRead: false
},
{
title: "War and Peace",
author: "Leo Tolstoy",
alreadyRead: true
}
]
for (var i=0; i<books.length; i++){
if (books[i].alreadyRead){
console.log("You have already read " + books[i].title);
}
else {console.log("You haven't read " + books[i].title)}
}
var myMovie = {
title: "Star Trek: Into Darkness",
duration: 137,
stars: ["Chris Pine","Zachary Quinto", "Zoe Saldana","Leonard Nemoy"]
}
function printMovie(movie){
var toPrint = "";
toPrint += movie.title + " lasts for " + movie.duration + " minutes. Stars: ";
toPrint += movie.stars.join();
toPrint += ".";
console.log(toPrint);
}
printMovie(myMovie);
var Cat = function (myName){
this.name = myName,
this.tiredness = 5, //10 is really tired, 0 is rested
this.hunger = 5, // 10 is really hungry, 0 is full
this.loneliness = 5, //10 is really lonely, 0 is not
this.happiness = 5, //10 is really happy, 0 is sad
this.litter = [],
this.sleep = function (numHours){
this.tiredness -= numHours;
if (this.tiredness > 7){
console.log("Your cat needs a nap. Leave her alone.");
}
},
this.feed = function() {
this.hunger = 0;
},
this.abandon = function (numHours){
this.hunger += numHours;
this.loneliness += 2*numHours;
this.happiness -= numHours;
this.tiredness -= numHours;
},
this.climbing = function(){
var num = Math.floor(Math.random()*10);
console.log("Your cat climbed "+num+" stories. It is now hungrier, and happier.")
this.hunger += num;
this.happiness += num/2;
},
this.pet = function(numPet) {
if (this.happiness > 8){
console.log("Your cat doesn't want to be pet. She bites you.");
this.happiness -= 1;
}
this.loneliness -= 1;
},
this.catnip = function() {
this.happiness = 10;
},
this.givebath = function() {
this.happiness -= 5;
},
this.play = function(){
this.tiredness += 1;
this.happiness += 2;
},
this.haveKittens = function(){
var num = Math.floor(Math.random()*3+3);
for(var i=0; i<num; i++){
var newKitten = new Cat("Kitten #"+(i+1));
this.litter.push(newKitten);
console.log("Congratulations, "+this.name+" just had "+newKitten.name);
}
this.tiredness += 2*num;
this.happiness += 3;
},
this.lickKittens = function() {
for(var k in this.litter){
this.litter[k].happiness += 2;
}
this.tiredness += this.litter.length;
this.happiness += 2;
},
this.nurseKittens = function() {
for(var k in this.litter){
this.litter[k].hunger = 0;
}
this.tiredness += this.litter.length;
},
this.eatKitten = function(){
var num = Math.floor(Math.random()*this.litter.length);
console.log("Oh noes! "+this.name+" ate poor little "+this.litter[num].name);
this.litter.splice(num,1);
this.hunger = 0;
for( var i=0; i<this.litter.length; i++){
this.litter[i].happiness -= 5;
this.litter[i].loneliness += 5;
}
},
this.printstatus = function(){
console.log(this.name+"'s tiredness level is: " + this.tiredness);
console.log(this.name+"'s hunger level is: " + this.hunger);
console.log(this.name+"'s loneliness level is: " + this.loneliness);
console.log(this.name+"'s happiness level is: " + this.happiness);
for (var k in this.litter){
this.litter[k].printstatus();
}
}
}
var myCat = new Cat("Xanadu");
var myKitty = new Cat("Bubbles");
myCat.printstatus();
myCat.haveKittens();
myCat.printstatus();
myCat.lickKittens();
myCat.eatKitten();
myCat.printstatus();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment