...

Exercise: Pizza Area Calculator

Remember that pizza party where you ordered 12-inch pizzas for everyone? Well, here's a plot twist: you've just discovered that one 18-inch pizza has more pizza than two 12-inch pizzas.

Armed with righteous indignation agianst Big Pizza, you decide to rage-code a pizza area calculator to ensure you always know how much pizza you're getting.

In this exercise, you will write a program that:

  • Asks the user for the diameter of the pizza (in inches) using the prompt function.
  • Calculates the radius of the pizza using the formula: radius = diameter / 2.
  • Calculates the area of the pizza using the formula: area = Math.PI * Math.pow(radius, 2).
  • Prints the area to the console in the format: "The area of the pizza is: AREA square inches".

Math.PI and Math.pow

As we saw earlier, JavaScript provides many useful mathematical values and functions in the Math object. Here are two you'll use in this exercise:

  • Math.PI: Represents the value of π (approximately 3.14159).
  • Math.pow(base, exponent): Calculates the power of a number. For example, Math.pow(5, 2) calculates 5 power 2, which evaluates to 25. You could also simply do 5 * 5, but we're using Math.pow just to try something new.

Now go write some code! Complete the code sample in the editor to calculate the area of a pizza and display the result in the format: "The area of the pizza is: AREA square inches"

Loading...

Output (Console)