Challenge 20 Megathread

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
totalVolume=s=>s.reduce((a,c)=>a+(c.type[0]=="p"?y(c.height,c.width,c.depth):(c.type<"d"?x:z)(c.radius,c.height)),0)

image

1 Like
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) {
  total = 0;
  for (part of solids) {
    switch (part.type) {
      case "sphere":
        total += sphereVolume(part.radius);
        break;
      case "cone":
        total += coneVolume(part.radius, part.height);
        break;
      case "prism":
        total += prismVolume(part.height, part.width, part.depth);
        break;
    }
  }
  return total;
};

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) {
  
  let sphere = 4/3 * PI * radius * radius * 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') {
      total += sphereVolume(solids[i].radius); 
    } else if (solids[i] === 'cone') {
      total += coneVolume(solids[i].radius, solids[i].height);
    } else if (solids[i] === 'prism') {
      total += prismVolume(solids[i].height, solids[i].width, solids[i].depth);
    } 
  }
  return totalVolume, total;
};

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 'sphere': return sphereVolume(solid.radius);
    case 'cone': return coneVolume(solid.radius, solid.height);
    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

Wrong info given :

Wrong info given :

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') {
      
      return volume += sphereVolume(solid.radius);
      
    } 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… :upside_down_face:

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') {
      total += sphereVolume(solids[i].radius);
    } else if ((solids[i].type === 'cone')) {
      total += coneVolume(solids[i].radius, solids[i].height);
    } else { //prism
      total += prismVolume(solids[i].height, solids[i].width, solids[i].depth);
    }
  }
  return total;
}

My solution for today:

Solution

1 Like

yeah I had to click every one of those links, havent calculated the volume of anything in like 15-20 years lol

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

1 Like