条件If语句更改方法

时间:2017-12-01 06:20:19

标签: javascript object if-statement

我有这个功能,它应该循环通过这些附加功能,而他们不会使总数超过100.但是,迭代停止,我需要它完成饥饿和危险值的任何一个100或0取决于哪个。通过的数字首先是:(50,50)秒:(0,100)。当if条件不再满足时,我似乎无法弄清楚如何更改加/减量。在重复条件值之前,我的控制台都显示了最后一次迭代。

问题是:

创建一个名为frodo的函数。 frodo将采用两个参数: startingHungerValue(Number)和startingDangerValue(Number)。 frodo需要将这些值存储在内部变量上。

然后,

frodo将返回一个具有两种方法的对象: 第一种方法叫做dinnerOverFire。 dinnerOverFire将减少饥饿25点,并将危险提高40点。

第二种方法称为hiddenInBush。 hiddenInBush会使饥饿感增加35点,并使危险值降低20点。

两种方法都需要返回如下结构的对象:

{   饥饿:(改变饥饿价值),   危险:(修改危险值) }

注意:饥饿和危险都不应超过100或低于0。

function frodo(startingHungerValue, startingDangerValue) {

  var shv = startingHungerValue;
  var sdv = startingDangerValue;

  console.log('startingHungerValue:', shv, 'startingDANGERvalue:', sdv)



  return {
    dinnerOverFire: () => {
      if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
        return {
          hunger: shv - 25,
          danger: sdv + 40
        }
      }
    },
    hidingInBush: () => {
      if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
        return {
          hunger: shv + 35,
          danger: sdv - 20
        }
      } 
    }
  }
}

2 个答案:

答案 0 :(得分:1)

不是你要求的,但我觉得你想做下面的事情。它会创建一个class佛罗多,您可以在eathide上调用方法。

您可以在此处看到它:jsfiddle

function Frodo(startingHungerValue, startingDangerValue) {
  var self = this; //this could change per scope, self keeps a reference to the basic of the class

  //store levels
  self.hunger = startingHungerValue;
  self.danger = startingDangerValue;

  //show levels method
  self.showLevels = function(){
    console.log('My hungerlevel: '+ self.hunger);
    console.log('My dangerlevel: '+ self.danger); 
    console.log('------'); 
  }

  //dinner method
  self.dinnerOverFire = function() {
    var newHunger = self.hunger - 25;
    var newDanger = self.danger + 40;

    if (newHunger<0) {
        newHunger = 0; //no negatives
    }

    if (self.hunger==0) {
        console.log('I\'m not hungry! No dinner!');
      console.log('------'); 
      return;       
    }
    if (newDanger>100) {
        console.log('Eating now would kill me! No dinner!');
      console.log('------'); 
      return;       
    }
    self.hunger = newHunger;
    self.danger = newDanger;

    console.log('Thanks for dinner!');
    self.showLevels();
  }

  //hiding method
  self.hideInBush = function() {
    var newHunger = self.hunger + 35;
    var newDanger = self.danger - 20;

    if (newDanger<0) {
        newDanger = 0; //no negatives
    }

    if (newHunger>100) {
        console.log('Hiding now would kill me! No hiding!');
      console.log('------'); 
      return;       
    }
    if (self.danger==0) {
        console.log('I\'m not scared at all! No hiding!');
      console.log('------'); 
      return;       
    }
    self.hunger = newHunger;
    self.danger = newDanger;

    console.log('Thanks, I feel safer already!');
    self.showLevels();
  }

  //initial message
  console.log('Hi, i\'m frodo!');
  self.showLevels();
}

//run your frodo
var frodo = new Frodo(50,50);
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();
frodo.hideInBush();
frodo.dinnerOverFire();

这将输出:

Hi, i'm frodo!
My hungerlevel: 50
My dangerlevel: 50
------
Thanks for dinner!
My hungerlevel: 25
My dangerlevel: 90
------
Thanks, I feel safer already!
My hungerlevel: 60
My dangerlevel: 70
------
Eating now would kill me! No dinner!
------
Thanks, I feel safer already!
My hungerlevel: 95
My dangerlevel: 50
------
Thanks for dinner!
My hungerlevel: 70
My dangerlevel: 90
------
Hiding now would kill me! No hiding!
------
Eating now would kill me! No dinner!
------

这已经显示了当前方式的问题。最后,他害怕吃东西,害怕躲起来。

答案 1 :(得分:0)

你的意思是,在if条件中添加一个else块吗?

function frodo(startingHungerValue, startingDangerValue) {
  var shv = startingHungerValue;
  var sdv = startingDangerValue;
  console.log('startingHungerValue:', shv, 'startingDANGERvalue:', sdv)
  return {
    dinnerOverFire: () => {
      if ((shv === 50 && shv === 50)){
        return {
          hunger: shv - 50,
          danger: sdv + 50
        }
      }
      else if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
        return {
          hunger: shv + 100,
          danger: sdv - 100
        }
      }
    },
    hidingInBush: () => {
      if ((shv === 50 && shv === 50)){
        return {
          hunger: shv - 50,
          danger: sdv + 50
        }
      }
      else if ((shv >= 0 && shv < 101) && (sdv >= 0 && sdv < 101)) {
        return {
          hunger: shv + 100,
          danger: sdv - 100
        }
      }
    }
  }
}

console.log(frodo(50, 50).dinnerOverFire()); // 0,100

console.log(frodo(0, 100).dinnerOverFire()); // 100,0
.as-console {
  height: 100%;
}

.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}