 Welcome to challenge 20! Use this thread for any and all questions relating to challenge 20.

Need help with challenge 20? Watch this video tutorial video Lighthouse Labs instructor Nima and Vancouver web development bootcamp student Gabi.

2 Likes

Late night code golf!

``````x=coneVolume=(r,h)=>r*r*h*1.047
y=prismVolume=(h,w,d)=>h*w*d
z=sphereVolume=r=>x(r,r)*4
`````` 1 Like
``````const PI = 3.14159;

return (4 / 3) * PI * Math.pow(radius, 3);
};

const coneVolume = function(radius, height) {
return (1 / 3) * PI * Math.pow(radius, 2) * height;
};

const prismVolume = function(height, width, depth) {
return height * width * depth;
};

const totalVolume = function(solids) {
total = 0;
for (part of solids) {
switch (part.type) {
case "sphere":
break;
case "cone":
break;
case "prism":
total += prismVolume(part.height, part.width, part.depth);
break;
}
}
};

``````
1 Like

Can someone help me? I cant find what is wrong, but my calculation of the total volume is not working.

This is my code:

``````const sphereVolume = function (radius) {

return sphere;

};

const coneVolume = function (radius, height) {

let cone = 1/3 * PI * radius * radius * height;
return cone;

};

const prismVolume = function (height, width, depth) {

let prism = height * width * depth;
return prism;

};

const totalVolume = function (solids) {

let total = 0;

for (let i = 0; i < solids.length; i++) {
if (solids[i] === 'sphere') {
} else if (solids[i] === 'cone') {
} else if (solids[i] === 'prism') {
total += prismVolume(solids[i].height, solids[i].width, solids[i].depth);
}
}
};
``````

You just have to return the total

my solution:

``````
// Use the value below whenever you need the value of Pi

const PI = 3.14159

const sphereVolume = function (radius) {
return 4 / 3 * PI * Math.pow(radius,3);
}

const coneVolume = function (radius, height) {
return PI * Math.pow(radius, 2) * height / 3
}

const prismVolume = function (height, width, depth) {
return height * width * depth;
}

const getVolume = (solid) => {
switch(solid.type){
case 'prism': return prismVolume(solid.height, solid.width, solid.depth);
default: return 0;
}
}

const totalVolume = function (solids) {
return solids.reduce((acc, solid) => acc + getVolume(solid), 0)
}
``````
2 Likes

There are a few typos in the challenge today:
but they have no clue how to much blow them up -> but they have no clue how much to blow them up.

#### Input:

``````    const prism = {
type: 'prism',
height: 3,
width: 4,
depth: 5
}

coneVolume(prism.height, prism.width, prism.depth) // SHOULD BE prismVolume
``````

Expected Output for the volume of duck should be 268083 (to 6 figures), not 49.8

For some reason, my solution is failing one test. Although I feel that my solution is correct? is there something that I am missing?

Failed: The totalVolume function returns the correct volume

Reason: Your code ran but did not produce the correct result.

Hint: Each of your volume function should return the volume.

"

``````// Use the value below whenever you need the value of Pi

const PI = 3.14159;

const sphereVolume = function (radius) {
return (4 / 3 ) * PI * Math.pow(radius, 3);

}

const coneVolume = function (radius, height) {
return (1 /  3) * PI * Math.pow(radius, 2) * height;
}

const prismVolume = function (height, width, depth) {
return height * width * depth;

}

const totalVolume = function (solids) {

let volume = 0;

for (let solid of solids) {
if(solid.type === 'sphere') {

} else if (solid.type === 'cone') {

return volume += coneVolume(solid.radius, solid.height) ;

} else {

return volume += prismVolume(solid.height, solid.width, solid.depth);
}
}

return volume;

}

``````

Any help will be greatlly appreciated. Thank you in advance.

Nevermind i figured out what the issue was lol

1 Like

This was a nice refresher on math problems I haven’t looked at in a while… ``````const PI = 3.14159

const sphereVolume = function (radius) {
return ((Math.pow(radius, 3) * PI) * (4/3));
}

const coneVolume = function (radius, height) {
return ((1/3) * (PI * Math.pow(radius, 2)) * height);
}

const prismVolume = function (height, width, depth) {
return height * width * depth;
}

const totalVolume = function (solids) {
let total = 0;

for (let i = 0; i < solids.length; i++) {
if (solids[i].type === 'sphere') {
} else if ((solids[i].type === 'cone')) {
} else { //prism
total += prismVolume(solids[i].height, solids[i].width, solids[i].depth);
}
}
Don’t forget that `totalVolume` is looking at an array of objects, so you’ve got to specify where the shape type can be found! Try replacing `solids[i]` with `solids[i].type` in the if statements.
And it only has to return `total`, not `totalVolume, total`