Challenge 20 Megathread

Here’s my solution. I got rid of the PI variable since Javascript has the constant built-in.

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

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

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

const getShape = (s) => {
  switch(s.type) {
    case "sphere" : return sphereVolume(s.radius);
    case "cone"   : return coneVolume(s.radius, s.height);
    case "prism"  : return prismVolume(s.height, s.width, s.depth);
  }
};

const totalVolume = function (solids) {
  let total = 0;
  solids.forEach(i=>{
    total += getShape(i);
  });
  return total;
};

This one was kind of fun.

const sphereVolume = radius =>
  4 / 3 * Math.PI * Math.pow(radius, 3)

const coneVolume = (radius, height) =>
  Math.PI * Math.pow(radius, 2) * height / 3

const prismVolume = (height, width, depth) =>
  height * width * depth

const totalVolume = solids =>
  solids.reduce((totalVolume, { type, height, width, depth, radius }) =>
    totalVolume + (
      type === 'cone' ?
        coneVolume(radius, height) :
          type === 'prism' ?
            prismVolume(height, width, depth) :
              type === 'sphere' ?
                sphereVolume(radius) :
                  0
    ),
    0
  )

In every if statement, you’re directly returning volume, you should only return volume all the way at the end (which you already do).

So just remove the return from the if/else statements :wink:

E.g.: return volume += sphereVolume(solid.radius); should be volume += sphereVolume(solid.radius);

Spelling mistakes ruin everything… it was right from the start but I mispelled prism… fixed the spelling everything is good.

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) {
  var total = 0;
  for(var solid of solids){
    if(solid.type =='sphere'){
      total+=sphereVolume(solid.radius);
    }else if(solid.type=='cone'){
      total += coneVolume(solid.radius,solid.height);
    }else if(solid.type=='prism'){
      total+= prismVolume(solid.height, solid.width, solid.depth);
    }
  }
  return total;
}
const PI = 3.14159;

const sphereVolume = radius => (PI * radius ** 3 * 4) / 3;

const coneVolume = (radius, height) => (PI * radius ** 2 * height) / 3;

const prismVolume = (height, width, depth) => width * height * depth;

const totalVolume = solids =>
  solids
    .map(solid => {
      if (solid.type == "sphere") return sphereVolume(solid.radius);
      if (solid.type == "cone") return coneVolume(solid.radius, solid.height);
      if (solid.type == "prism") return prismVolume(solid.height, solid.width, solid.depth);
    })
    .reduce((a, b) => a + b, 0);

This is fun!!

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

const PI = 3.14159

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

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

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

const calculatorFactory = shape => {
    const { type, ...otherProps } = shape;
    const calculators = {
        'sphere': sphereVolume,
        'cone': coneVolume,
        'prism':  prismVolume
    }

    return calculators[shape.type](...Object.values(otherProps));
}


const totalVolume = function (solids) {
    const total = solids.reduce((acc, currentValue) => {
        return acc + calculatorFactory(currentValue)
    }, 0);

    return +total.toFixed(3)
}

Since this challenge is designed for people learning Javascript (and you are clearly not a novice), if you added a some explanation of the more advanced features and coding paradigms that you use, I think the reset of us could benefit. Thx.

Glad you enjoyed it :smiley:.

Here’s my solution:

const PI = 3.14159 

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

const coneVolume = function (radius, height) {
  return isNaN(radius) || isNaN(height) ? 0.0 : PI * Math.pow(radius,2) * height / 3;
}

const prismVolume = function (height, width, depth) {
  return isNaN(height) || isNaN(width) || isNaN(depth) ? 0.0 :
    width * depth * height;
}

const totalVolume = function (solids) {
  var vol = 0.0;
  try {
      solids.forEach( item => {
        switch (item.type) {
          case 'sphere' :
            vol += sphereVolume( item.radius );
            break;
          case 'cone':
            vol += coneVolume( item.radius, item.height );
            break;
          case 'prism':
            vol += prismVolume( item.height, item.width, item.depth );
            break;
        }
      });
  } catch(ex) {
    console.log( ex );
    vol = 0.0;
  }
  return vol;
};

Finally broke down and learned how to use a reduce to sum up my total volumes. Great challenge today, definitely the type of problem I would expect from the penultimate challenge!

1 Like

I made a mistake that was accepted anyways, I didn’t notice PI was supplied so i used Math.PI instead. got all correct answers still.

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

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

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

const totalVolume = function (solids) {
  total = 0
  solids.map(solid => {
  switch(solid['type']){
    case "sphere":
      total += sphereVolume(solid['radius']);
      break;
    case "cone":
      total += coneVolume(solid['radius'], solid['height']);
      break;
    case "prism":
      total += prismVolume(solid['height'],solid['width'],solid['depth']);
      break;
  }  
  })
  
  
  //console.log(total);
  return total;
}

Same as @injust pretty much.

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

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

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

const totalVolume = function (solids) {
  return solids.reduce((acc, s) => {
    return s.type == "sphere" ? acc + sphereVolume(s.radius) :
    s.type == "cone" ? acc + coneVolume(s.radius, s.height):
    s.type == "prism" ? acc + prismVolume(s.height, s.width, s.depth): acc
  }, 0)
}

Thanks for helping me =)

1 Like

I was able to pass the submission test, but it’s failing one of the “Test Code” tests. The error is:

Failed: The totalVolume function returns the correct volume
Reason: Your code could not run. “solids.forEach is not a function”

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

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

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

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

  solids.forEach(s => {
    if (s.type === 'sphere') {
      volTotal += sphereVolume(s.radius);
    } else if (s.type === 'cone') {
      volTotal += coneVolume(s.radius, s.height);
    } else if (s.type === 'prism') {
      volTotal += totalVolume(s.height, s.width, s.depth);
    }
  });

  return volTotal;
};

Also in hindsight, I should’ve used a switch instead of if/else.

const PI = 3.14159;

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

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

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

const totalVolume = function(solids) {
  return solids.reduce((acc, {radius, height, width, depth, type}) => {
    switch(type) {
      case 'sphere': return acc += sphereVolume(radius);
      case 'cone': return acc += coneVolume(radius, height);
      case 'prism': return acc += prismVolume(height, width, depth);
      default: return acc;
    }
  }, 0);
};

Here is my answer

I used Math.pow for calculating exponents and a switch statement for totalVolumes().

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

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

const prismVolume = function (height, width, depth) {
  // V = Bh
  return (width * depth) * height
}

const totalVolume = function (solids) {
  var volume = 0

  solids.forEach(solid => {
    switch(solid.type) {
      case 'sphere':
        volume += sphereVolume(solid.radius)
        break
      case 'cone':
        volume += coneVolume(solid.radius, solid.height)
        break 
      default: // prism
        volume += prismVolume(solid.height, solid.width, solid.depth)
    }
  })

  return volume
}

try Object.values(solids).forEach instead

same code here, I had strange problem while working on it though, though I’m sending shape object to VolumeCalc() function, when I check the variable value inside the function I would get [Object, Object]
not sure what caused it and not sure what solved it, anyone here has Idea?

const PI = 3.14159
const sphereVolume = function (radius) {
return 4 * PI * Math.pow(radius,3)/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 VolumeCalc = function (shape) {
switch (shape.type) {
case ‘sphere’:return sphereVolume(shape.radius);
case ‘cone’:return coneVolume(shape.radius,shape.height);
case ‘prism’:return prismVolume(shape.height,shape.width,shape.depth);
}
}
const totalVolume = function (solids) {
return solids.reduce((total,shape)=>{return total+VolumeCalc(shape);},0);
}

Any idea why I do not get the correct total.

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

const PI = 3.14159;

const sphereVolume = function (radius) {
//console.log(radius);
radius = Math.abs(radius);
volume = (4/3) * Math.PI * Math.pow(radius, 3);
volume = volume.toFixed(4);

var Volume = Number(volume);

return Volume;

};

const coneVolume = function (radius, height) {

return PI * radius * radius * (height / 3);
};

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

};

const totalVolume = function (solids) {
var total = 0;
var Total = 0;

for (const solid of solids){
console.log("type: ",solid.type);

if(solid.type === “sphere”){
total += sphereVolume(solid.radius);
// console.log("A: ", total);
}else if(solid.type==‘cone’){

 total += coneVolume(solid.radius, solid.height);

// console.log("B: ", total);
}else if(solid.type==‘prism’){

  total += prismVolume(solid.height, solid.width, solid.depth);

}
console.log("total: ", total);

Total = Number(total);

return Total;

}

};

your return value is inside the for loop
Here is the fix:

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

const PI = 3.14159;

const sphereVolume = function (radius) {
//console.log(radius);
radius = Math.abs(radius);
volume = (4/3) * Math.PI * Math.pow(radius, 3);
volume = volume.toFixed(4);

var Volume = Number(volume);

return Volume;

};

const coneVolume = function (radius, height) {

return PI * radius * radius * (height / 3);
};

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

};

const totalVolume = function (solids) {
var total = 0;
var Total = 0;

for (const solid of solids){
console.log("type: ",solid.type);

if(solid.type === ‘sphere’){
total += sphereVolume(solid.radius);

}else if(solid.type===‘cone’){

total += coneVolume(solid.radius, solid.height);

}else if(solid.type===‘prism’){

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

}
console.log("total: ", total);

Total = Number(total);

return Total;
};