Using Modulo to calculate remainder
//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.");
the question:
At the pizza party Sparky, the host's dog is excited, because he gets the leftover pizza after the slices have been divided up evenly among the guests. Assume guests get whole slices, how many whole slices will Sparky feast on? Example data set: 10 people, 4 pizzas and 8 slices per pizza will mean each person eats 3 slices and Sparky gets 2 slices. (Note that this is an example, your code should work and give me the accurate results no matter what numbers I put in for those given variables.) Given: Don't make new given variables/constants for this. Instead use the givens you set up for Slice of Pie I. Result Variables: Number of slices Sparky gets to eat. Result to Print: “Sparky got X slices of pizza.”
I've plugged and tried as many methods as I could think of but I can get it to work with different numbers. It's always off by a tiny bit.
var leftovers = (slicesPerPizza * numberOfPizzas) % numberOfGuests;
var slicesPerPerson = (slicesPerPizza * numberOfPizzas - leftovers) / numberOfGuests;
每个客人都得到slicesPerPerson,而Sparky获得剩菜。
链接地址: http://www.djcxy.com/p/58290.html上一篇: 推导模态表达式的数学方法
下一篇: 使用Modulo来计算余数