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

Here is my solution. Critiques very welcome!

I added a little error checking to my solution.

/**
* Calculate the area of a rectangle
*
* @param {number} width - The width of the rectangle
* @param {number} length - The height of the rectangle
* @returns {number} The area and unit of measurement
*/

const getArea = (width, length) => {
  if(isNaN(width) || isNaN(length)) {
    throw Error('Width and Length values must be a number.');
  } else if (width == 0 || length == 0) {
    throw Error('Cannot multiply by zero.');
  } else {
    return width * length;
  }
}


/**
* Calculate the volume of a rectangular prism
*
* @param {number} width - The width of the rectangle
* @param {number} length - The length of the rectangle
* @param {number} height - The height of the rectangle
* @returns {number} The volume of the rectangular prism
*/

const getVolume = (width, length, height) => {
  if(isNaN(width) || isNaN(length) || isNaN(height)) {
    throw Error('Function can only accept numbers as arguments.');
  } else if(width == 0 || length == 0 || height == 0) {
    throw Error('Cannot multiply by zero.');
  } else {
    return width * length * height;
  }
}


/**
* Calculate the area of a circle
*
* @param {number} radius - The radius of the circle
* @returns {number} The area of the circle
*/

const getCircleArea = radius => {
  if(isNaN(radius)) {
    throw Error('Arguement must be a number value.');
  } else if (radius == 0) {
    throw Error('Cannot multiply by zero.');
  } else {
    return Math.PI * Math.pow(radius, 2);
  }
}


/**
* Calculate the volume of a sphere
*
* @param {number} radius - The radius of the sphere
* @returns {number} The volume of the sphere
*/

const getSphereVolume = radius => {
  if (isNaN(radius)) {
    throw Error('Function only accepts number values as arguments.');
  } else if (radius == 0) {
    throw Error('Cannot multiply by zero.');
  } else {
    return 4 / 3 * Math.PI * Math.pow(radius, 3);
  }
}

// Output
console.log(getArea(5, 22));
console.log(getVolume(4.5, 12.5, 17.4));
console.log(getCircleArea(7.2));
console.log(getSphereVolume(7.2));