【问题标题】:Cannot Get Function to Execute Properly无法正确执行函数
【发布时间】:2017-07-06 17:39:13
【问题描述】:

我正在尝试测试我写的一些代码,但我无法使用“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 能量,如果它们没有足够的能量它们会说“猴子太累了”。

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

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

【问题讨论】:

  • 您在分配其他变量之前调用了jungleSoundOff
  • 另外,new function () {} 是一种反模式,而且构造函数甚至不应该是构造函数。
  • tigerFood = {meat, bugs, fish}; 应该是什么?这是{meat: beat, bugs: bugs, fish: fish} 的 ES6 简写,但您从未定义过这些变量。
  • 然后你会做Math.random(tigerFood + energy + 5),但tigerFood 不是一个数字,它是一个对象。为什么要添加它?
  • 你的代码中有很多未声明的变量,我就不一一列举了。请提供一个完整的例子。

标签: javascript function javascript-objects


【解决方案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.");
}

【讨论】:

  • 远不远,这段代码在很多层面上都好得多。并且具有明显的优势,即它确实有效。
最近更新 更多