javascript - 无法让函数正确执行

标签 javascript function javascript-objects

我正在尝试测试我写的一些代码,但我无法让函数“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 sleep

丛林可以发出声音,动物会发出声音并报告能量水平。

老虎 sleep 时获得+5能量。 猴子进食时获得 +2 能量,发声时获得 -4 能量

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

老虎不能吃 Cereal ,因为它们的胃很敏感。

丛林可以让每只动物执行该动物可能进行的随机事件。

最佳答案

我尝试逐步更改您的代码,直到它按照您所描述的方式进行。不幸的是,我最终得到了一些遥远的东西。我希望你能从我解决问题的方法中得到一些东西。我决定不实现 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.");
}

关于javascript - 无法让函数正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955638/

相关文章:

javascript - 使用 CSS 媒体查询选择 JavaScript 元素

javascript - Sails.js 1.0 的嵌套属性 where 子句不再起作用

javascript - 分配给 {}.toString 是什么意思?

用函数替换多个 `summarize` 语句

javascript - 克隆一个 div 并在 div 中获取特定 id 的值

javascript - 将唯一 id 添加到深层嵌套对象数组的最有效方法

javascript - 理解 typescript 生成的 __extends 函数?

javascript - 是否可以根据方向变化隐藏/显示div javascript/query

javascript - 动态添加语义 UI 控件的正确方法?

python - 从 re.sub 调用函数