2017-07-06 77 views
0

我试图测试一些我写出来的代码,但是我无法获取函数“jungleSoundOff”来实际记录我要求的内容。我试着将它从最底层移动到提示下,但它仍然不记录我创建的其他动物功能的任何内容。我是否以这种错误的方式去做?还是我错过了一些非常基本的东西?无法正常执行函数

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); 
 
} 
 
} 
 
} 
 
}

程序应该做什么:

能级: -3作出合理 +5吃的食物 +10睡觉

丛林听起来关闭,动物会发出声音并报告能量水平。

老虎获得+5能量供睡觉。 猴子吃得到+2能量和发出声音-4能量

只有当猴子说他们说“oooo ooooo ooooo”时他们可以玩,如果他们没有足够的能量,他们会得到-8能量“猴子太累了。”

老虎不能吃谷物,因为他们有敏感的胃。

丛林可以让每只动物对该动物进行随机活动。

+0

你调用'jungleSoundOff'您指定的其他变量之前。 – Barmar

+1

另外,做'new function(){}'是一个反模式,以及构造函数甚至不应该是构造函数。 – Li357

+0

什么是'tigerFood = {肉,臭虫,鱼};'应该是?这是ES6对'{肉类:节拍,虫子:虫子,鱼类:鱼类}'的简写,但你从来没有定义过这些变量。 – Barmar

回答

1

我试着一步一步地改变你的代码,直到它做了你所描述的。不幸的是,我结束了很远的事。我希望你能从我对问题的解决方法中获得一些东西。我决定不实施老虎,所以你可以自己实施。

// 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."); 
 
}

+0

这个代码在很多层面上都好得多,但不是很好。它有明显的优势,它实际上工作。 – Bergi