0
    //How much pizza each party goer will recieve
    const slices = 3; //Number of slices 8
    var people = 8; //Number of people 25
    var pizzas = 10; //Number of pizzas 10

   //pizzas ordered times the number of slices, divided by the number of people & asign         slicePerson variable
   var slicePerson = (slices * pizzas)/people;
  //print out how many pieces per person
  console.log("Each person ate" + " " + slicePerson + " " + "slices of pizza at the party.")



  //Sparky gets what remainder of pizza
  //Slices by pizzas used modulo to give remainder with people
  var dogFood = 30 % people;
  //print out how many slices sparky got
 console.log("Sparky got" + " " + dogFood + " " + "slices of pizza.");

问题:

在比萨派对 Sparky 上,主人的狗很兴奋,因为在客人们平均分配切片后,他得到了剩下的比萨。假设客人得到整片,Sparky 会吃多少整片?示例数据集:10 人,4 个披萨,每个披萨 8 片,这意味着每个人吃 3 片,Sparky 得到 2 片。(请注意,这是一个示例,无论我为这些给定变量输入什么数字,您的代码都应该工作并给我准确的结果。) 给定:不要为此创建新的给定变量/常量。而是使用您为 Slice of Pie I 设置的给定值。 结果变量:Sparky 可以吃的切片数。打印结果:“Sparky 得到 X 片披萨。”</p>

我已经插入并尝试了尽可能多的方法,但我可以让它使用不同的数字。它总是偏离一点点。

4

1 回答 1

0
var leftovers = (slicesPerPizza * numberOfPizzas) % numberOfGuests;
var slicesPerPerson = (slicesPerPizza * numberOfPizzas - leftovers) / numberOfGuests;

每个客人都得到 slicesPerPerson,Sparky 得到剩菜。

于 2013-11-05T20:57:46.393 回答