【问题标题】:Pushing Objects From Function Factories into Arrays将对象从函数工厂推入数组
【发布时间】:2018-10-08 14:30:32
【问题描述】:

我做了两个函数工厂,一个用于僵尸,一个用于人类。我想要一个函数来计算僵尸总生命值与人类总攻击之间的差异。它每个都可以工作,但我无法理解如何做多个人类与僵尸。我尝试将人类对象推到一个数组中,以便我可以总结所有攻击(我会为僵尸重复),但没有运气......

//Create a Human spawning object
var humanArr = [];
const humanSpawns = (attack) => {
    let human = {
        attack: attack
    };
    humanArr.push(human);
};


//Create a Zombie spawning object
const zombieSpawns = (health) => {
    return {
        health: health
    }
};


//Create function to have humans and zombies fight
function fight() {
   var result = humanOne.attack - zombieOne.health;
   if(result > 0) {
       document.write('Live to see another day.');
   } else {
       document.write('The zombies are taking over!');
   }
}

const zombieOne = zombieSpawns(12);
const humanOne = humanSpawns(11);
fight();

【问题讨论】:

    标签: function object factory


    【解决方案1】:

    尝试类似于我的 sn-p 的东西,你真正需要的是一种创建单位的方法。 我使用了普通对象,但如果你想返回casualties,比方说,你需要为每个主要对象返回一个humanoids的数组,并根据受到的伤害在战斗中返回splice它们。放轻松,你会看到你的军队战斗的!

    我追求的逻辑:

    1. 我需要一支军队!

      1.1。要创建军队,首先我需要一些单位,所以我构建了createHumanoid(应该将其重命名为 createArmy)

      1.2。 createHumanoid 将帮助设置单位的一些属性以及我的军队中有多少人。

    2. 创建一个armies 数组,我将在其中使用createHumanoid 创建我的军队

    3. 我需要知道军队有多强大,所以我构建了getArmyPower,它返回军队的namepower,这将用于4.2.

    4. 战斗开始! (favoriteArmy = 'humans')

      4.1。正在创建fight 方法并接受两个参数,第一个是armies,第二个是favoriteArmy

      4.2。使用.map 方法我将getArmyPower 应用于我的每个军队(数组的元素)以了解他们的力量

      4.3。然后我使用.sort 将它们按army.power 降序排序

      4.4。 let victorious = armies[0]; 会让我得到排序数组中的第一个元素。权力最高的那个。或者您可以使用destructuring 并将其写成let [victorious] = armies;(表示数组中的第一个元素)

      4.5。我比较了victorious.namefavoriteArmy 来检查我感兴趣的军队是赢了还是输了。

    /**
     * A function to create units
     */
    const createHumanoid = (race, howMany, stats) => {
    
      return {
        name: race,
        armySize: howMany,
        unitStats: stats
      }
    
    };
    
    /**
     * Register the armies into battle
     */
    let armies = [
    
      createHumanoid('humans', 12, {
        health: 10,
        attack: 12
      }),
      createHumanoid('zombies', 5, {
        health: 30,
        attack: 12
      }),
    
    ]
    
    /**
     * Get the max power of each army - you can adjust the algorithm
     */
    const getArmyPower = (army) => {
      return {
        name: army.name,
        power: +army.armySize * (+army.unitStats.health + +army.unitStats.attack)
      }
    
    }
    
    /**
     * Let them fight and see what your favorite army did in the battle
     */
    function fight(armies, favoriteArmy) {
    
      armies = armies
        .map(army => getArmyPower(army))
        .sort((a, b) => b.power - a.power);
    
      let victorious = armies[0];
    
      if (victorious.name.toLowerCase() === favoriteArmy.toLowerCase()) {
        document.write('Live to see another day.');
      } else {
        document.write('The zombies are taking over!');
      }
    }
    
    fight(armies, 'humans');

    【讨论】:

    • 这太棒了!你的代码比我的更流畅,哈哈。我确实有几个问题,因为我没有这么先进。 1.power实现了什么?我以前从未见过 + 这样的变量。 2. 为什么一定要加name: army.name,看起来好像在做什么。 3.我知道.map和.sort理论上是做什么的,但是我不确定它们在这里是如何应用的。
    • ‘Power’返回一个基于军队状态的数字,‘howManyUnits * (unit.health + unit.attack)’。在'map'方法中,'armies'数组是通过对'armies'中的每个项目应用'getArmyPower'方法来格式化的,然后我'排序'新格式化的数组'按幂降序',所以最大的军队' power' 将是第一个,然后我在“victorious”变量中检索数组的第一项。
    • '+' 将字符串转换为数字,这是我确保它们是数字的习惯,正如您所见,在 'name' 旁边,每个道具都是数字 :)
    • .map(army => getArmyPower(army)) -> army 是正在循环的项目,我可以写 .map( i => getArmyPower(i)) ,它只是正在处理的项目的别名。 favoriteArmy 用作fight(armies, favoriteArmy) 方法的参数。我在if 条件下的相同函数中使用它。 favoriteArmy 是我有兴趣看到“它在战斗中做了什么”的军队名称,将其更改为“僵尸”,你会得到“僵尸正在接管!”回答。在每个方法/变量上使用console.log 来查看它们的结构/输出顺序
    • 我已经更新了我的答案,以便您可以跟进上述脚本的开发。
    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 2023-03-14
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2011-02-13
    • 2012-10-21
    相关资源
    最近更新 更多