2017-07-06 7 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鳴らすことができます

ジャングル睡眠のための食糧 10を食べるために音 5を作るための動物は音を出し、エネルギーレベルを報告する。

虎は睡眠のために+5のエネルギーを得る。 サルは食べるのに+2のエネルギーを、音を作るためにエネルギーを得ます。

サルは「オオオオーオーーーーー」と言って、エネルギーが足りないと遊ぶことができます。 「サルはあまりにも疲れている」

虎は敏感な胃があるので穀物を食べることができません。

ジャングルは、各動物がその動物にとって可能なもののランダムな活性を有することができる。

+0

あなたが他の変数を割り当てる前に、 'jungleSoundOff'を呼んでいます。 – Barmar

+1

また、 'new function(){}'を実行することは、コンストラクタがコンストラクタであるとは思われないという事実とともに反パターンです。 – Li357

+0

'tigerFood = {肉、虫、魚}とは何ですか? ES6は '{肉:ビート、バグ、魚、魚}'の略語ですが、決してそれらの変数を定義しませんでした。 – Barmar

答えて

1

あなたのコードを、あなたが記述したものになるまで段階的に変更しようとしました。残念ながら、私は遠くの何かで終わった。私はあなたが問題への私のアプローチから何かを得ることができることを願っています。私はあなたがそれを自分で実装できるように、Tigerを実装しないことに決めました。

// 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

関連する問題