2017-07-06 45 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