Skip to content

Instantly share code, notes, and snippets.

@cplpearce
Created August 9, 2020 00:21
Show Gist options
  • Save cplpearce/1288658f955ba28daf1e0d81c868529a to your computer and use it in GitHub Desktop.
Save cplpearce/1288658f955ba28daf1e0d81c868529a to your computer and use it in GitHub Desktop.
Kata 9 - Bouncy Castles
// Use the value below whenever you need the value of Pi
const PI = 3.14159 ;
const sphereVolume = function (radius) {
// V = 4/3 πr³
return ((4/3) * (PI * Math.pow(radius, 3)));
}
console.log(4186 < sphereVolume(10) && sphereVolume(10) < 4189);
const coneVolume = function (radius, height) {
// V=πr2h/3
return ((PI * Math.pow(radius, 2) * (height / 3)));
}
console.log(45 < coneVolume(3, 5) && coneVolume(3, 5) < 49);
const prismVolume = function (height, width, depth) {
// height * width * depth
return (height * width * depth);
}
console.log(prismVolume(3, 4, 5) === 60);
const totalVolume = function (solids) {
let totalVolume = 0;
for (const part of solids) {
switch (part.type) {
case 'sphere':
totalVolume += sphereVolume(part.radius);
break;
case 'cone':
totalVolume += coneVolume(part.radius, part.height);
break;
case 'prism':
totalVolume += prismVolume(part.height, part.width, part.depth);
break;
default:
break;
}
}
return totalVolume;
}
const largeSphere = {
type: 'sphere',
radius: 40
}
const smallSphere = {
type: 'sphere',
radius: 10
}
const cone = {
type: 'cone',
radius: 3,
height: 5
}
const duck = [
largeSphere,
smallSphere,
cone
]
console.log(272000 < totalVolume(duck) && totalVolume(duck) < 275000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment