Reputation: 65
I'm trying to test some code I wrote out but I can't get the function "jungleSoundOff" to actually log what I'm asking it to. I've tried moving it from the very bottom to just under the prompt but it still does not log anything from the other animal functions I created. Am I going about this the wrong way? Or am I just missing something very basic?
var soundOff = prompt("Would you like to perform a jungle sound off?");
if(soundOff === "yes" || soundOff === "Yes"){
jungleSoundOff();
}else if(soundOff === "no" || soundOff === "No"){
console.log("Maybe another time.");
}else{
console.log("I don't understand your response.");
}
function tigerActivity(){
var energy = 0;
tigerFood = ["meat", "bugs", "fish"];
tigerEat = Math.floor(Math.random(tigerFood) + energy + 5);
tigerSleep = energy + 5;
var tigerSound = "roar";
function monkeyActivity(){
var energy = 0;
monkeyFood = ["meat", "bugs", "grain", "fish"];
monkeyPlay = energy - 8;
monkeyEat = Math.floor(Math.random(monkeyFood) + energy + 2);
var monkeySound = "oooo oooo";
function snakeActivity(){
var energy = 0;
snakeFood = ["meat", "bugs", "grain", "fish"];
snakeEat = Math.floor(Math.random(snakeFood) + energy + 5);
var snakeSound = "hiss";
function jungleSoundOff(){
console.log(tigerSound, energy);
console.log(monkeySound, energy);
console.log(snakeSound, energy);
}
}
}
}
What the program should do:
energy levels: -3 for making sound +5 for eating food +10 for sleeping
jungle can sound off, animals make sound and report energy level.
tigers get +5 energy for sleeping. monkeys get +2 energy for eating and -4 energy for making a sound
only monkeys can play when they do they say "oooo ooooo ooooo" and get -8 energy, if they don't have enough energy they say "Monkeys is too tired."
tigers can't eat grain because they have sensitive stomachs.
jungle can have each animal perform a random activity of the ones possible for that animal.
Upvotes: -1
Views: 74
Reputation: 345
I tried to change your code step by step until it did what you described. Unfortunately, I ended up with something far away. I hope you can get something out of my approach to the problem. I decided to not implement the Tiger so you could implement it yourself.
// We define how each animal by default would behave here and then we change specific behaviour later on.
class Animal {
constructor() {
this.energy = 0;
this.sound = "Default sound";
// Declare all actvities that animals are able to do
// bind this on the function so we can call them
this.activities = [
// bind the food bugs so they aren't eating air
this.eat.bind(this, "bugs"),
this.makeSound.bind(this),
this.sleep.bind(this)
];
}
eat(food) {
// Eating increases energy by 5
this.energy += 5;
console.log("Eating", food)
}
makeSound() {
// Make sound decreases energy by 3
this.energy -= 3;
console.log(this.sound, "and my energy is", this.energy)
}
sleep() {
// Sleeping increases energy by 10
this.energy += 10;
console.log("Sleep")
}
doRandomActivity(){
// We generate a random number between the number of activites
var actvityIndex = Math.floor(Math.random() * this.activities.length);
// get the random activity
var activity = this.activities[actvityIndex];
// call the activity
activity();
}
}
// We extend from the general animal so we can do all of the basic animal things
class Monkey extends Animal{
constructor() {
super();
this.sound = "oooo oooo";
// add the play activity to actvities, so it can done by a random action
this.activities.push(this.play.bind(this));
}
eat(food) {
// Since monkeys gain different amount of energy
this.energy += 2;
console.log("Eating", food)
}
makeSound() {
// Since monkeys make sounds differently than other animals we override the function
this.energy -= 4;
console.log(this.sound, "and my energy is", this.energy)
}
play() {
// Added the new play ability for the monkey
if (this.energy >= 8) {
console.log("oooo ooooo ooooo")
this.energy -= 8;
} else {
console.log("Monkeys is too tired");
}
}
}
// Snake extends animal so it can do all the animal things
// since the only special thing about the snake we only change its sound
class Snake extends Animal{
constructor(){
super();
this.sound = "hiss";
}
}
class Jungle {
constructor() {
// initialize animals array that contains all animals in the jungle
this.animals = [];
}
addAnimal(animal){
// add an animal
this.animals.push(animal);
}
soundOff(){
for(var i = 0; i < this.animals.length; i++) {
// go through all animals in the jungle and makeSound
this.animals[i].makeSound();
}
}
}
// create a the jungle jungle
var jungle = new Jungle();
// add our animals to the jungle
jungle.addAnimal(new Snake());
jungle.addAnimal(new Monkey());
var soundOff = prompt("Would you like to perform a jungle sound off?").toLowerCase();
if(soundOff === "yes"){
jungle.soundOff();
}else if(soundOff === "no"){
console.log("Maybe another time.");
}else{
console.log("I don't understand your response.");
}
Upvotes: 1