Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Daniel Springer
PLUS
Daniel Springer
Courses Plus Student 5,090 Points

Completed Project: Please comment if you want to. Any suggestions / improvements are welcome!

code

// 1. Attach this file geometry.js to the index.html file

// 2. Create a function that calculates the area of a rectangle.
//    The function should accept the width and height as arguments
//    and return the area of that rectangle.
//    The area of a rectangle is the width * height
var message;
var totalArea;
var totalVolume;
var circleArea;
var sphereVolume;

function area(width, height){

  while(isNaN(width)){
    width = prompt("what is the width of the circle?")
    width = parseFloat(width)

    if(isNaN(width)){  
      alert("The width input is not a number, try again")
    }
  }
  while(isNaN(height)){
    height = prompt("what is the height of the circle?")
    height = parseFloat(height)

    if(isNaN(height)){
       alert("The height input is not a number, try again")
    } 
  }
  return totalArea = width * height
} 
alert("The area of the circle is " + area())
console.log(area)



// 3. Create a function that calculates the volume of a rectangular prism.
//    The function should accept the width, height and length as arguments
//    and return the volume of that rectangular prism.
//    The volume of a rectangular prism is the width * height * length

function volume (width, height, lenght){

  while(isNaN(width)){
  width = prompt("what is the width of the rectangular prism?")
  width = parseFloat(width)

  if (isNaN(width)){
   alert("The width input is not a number, try again")
    }
  }
  while(isNaN(height)){
  height = prompt("what is the height of the rectangular prism?")
  height = parseFloat(height)

  if (isNaN(height)){
   alert("The height input is not a number, try again")
    }
  }
  while(isNaN(lenght)){
  lenght = prompt("what is the lenght of the rectangular prism?")
  lenght = parseFloat(lenght)

  if (isNaN(lenght)){
   alert("The lenght input is not a number, try again")
    }
  }
  return totalVolume = Math.round(width * height * lenght)
}
alert("The volume of the rectangular prism " + volume())
console.log(volume)


// 4. Create a function that calculates the area of a circle.
//    The function should accept the radius of the circle as an argument
//    and return the area of that circle.
//    The area of a circle is the value of π * radius^2

function circleArea(radius){

  while(isNaN(radius)){
    radius = prompt("What is the radius of your circle?")
    radius = parseFloat(radius)

    if(isNaN(radius)){
      alert("The height input is not a number, try again")
    }
  }
 return circleArea = Math.round(Math.PI * (radius * radius))
}
alert("The area of the circle is " + circleArea())
console.log(circleArea)

// 5. Create a function that calculates the volume of a sphere.
//    The function should accept the radius of the sphere as an argument
//    and return the volume.
//    The volume of a circle is: 4/3 *  π * radius^3

function sphereVolume(radius){

  while(isNaN(radius)){
    radius = prompt("What is the radius of your sphere?")
    radius = parseFloat(radius)

    if(isNaN(radius)){
      alert("The height input is not a number, try again")
    }
  }
 return sphereVolume = Math.round( 4 / 3 * Math.PI * (radius * radius * radius))
}
alert("The area of the circle is " + sphereVolume())
console.log(sphereVolume)

// 6. Use console.log to test each function and output to the JavaScript console
//    Here are the values to test and the expected results
//    -- Area of rectangle that is 5 wide and 22 tall: 110
//    -- Volume of a rectangular prism that is 4.5 x 12.5 x 17.4: 978.7499999999999
//    -- Area of a circle that with a radius of 7.2: 162.8601631620949
//    -- Volume of a spehere with a radius of 7.2: 1563.4575663561109

message = "<h1>Here are your results:</h1>"
message += "<br>"
message += "<h3>The area of the circle is " + totalArea + "</h3>";
message += "<h3>The volume of the rectangular prism is " + totalVolume + "</h3>";
message += "<h3>The area of the circle is " + circleArea + "</h3>";
message += "<h3>The area of the circle is " + sphereVolume + "</h3>";

document.write(message);

I think you'll get more replies if you link to some working code!