...

Exercise: Pizza Party Planner

You're hosting a pizza party, and you need to calculate how many pizzas to order based on the number of guests, and the number of slices a guest will eat.

In this exercise, you will write a program that:

  • Asks the user for the number of guests and the number of slices each guests will eat using the prompt function.
  • Calculates the number of pizzas to order, assuming each pizza has 8 slices.
  • Prints "You need to order X pizzas." to the console.

Math.ceil

Imagine you have 6 guests, and you're expecting them to eat 3 slices each. That's 18 slices. How many 8-slice pizzas do you need to order? 18 divided by 8 is 2.25!

Since we can't actually order 2.25 pizzas, we'll just go ahead and order 3 pizzas - and that's where Math.ceil comes in.

Math.ceil is a function that rounds up up a given number to the nearest whole number:

  • Math.ceil(2.25) evaulates to 3.
  • Math.ceil(3.8) evaulates to 4.
  • Math.ceil(5) evaulates to 5.

Math fun facts

  • The ceil in Math.ceil stands for ceiling. This makes it easier to remember.
  • There are a LOT of Math. functions available in Javascript. Math.floor rounds down to the nearest whole number, Math.round rounds up or down to the nearest whole number, Math.sqrt calculates the square root, and I could go on and on.
  • Check out the Math documentation on MDN for a list of everything available in the Math object. Since no developer on earth* can remember even half the functions a language provides, developers refer to resources like MDN docs very frequently.

* Even a developer in space can't remember half the functions a language provides. In fact, it's probably harder to remember Math function names in space because you likely have bigger problems to worry about if you're a developer that somehow ended up in space.

Loading...

Output (Console)