0

I have a javascript object which describes the radius of a hemisphere and the volume. I'd like to be able to change the radius and then access the volume with hemisphere.volume and it would return the volume for the current radius. I thought something like this would be possible but I'm getting NaN back from it when I call hemisphere.volume from the console.

var hemisphere ={radius:6,volume:Math.pow(this.radius,3)*4*Math.PI/6};

Then I thought I could do this as a function like:

var hemisphere ={radius:6,volume:function({Math.pow(this.radius,3)*4*Math.PI/6}};

This gives me 'undefined' when I call problem.answer() from the console.

Any ideas how I can do this?

melpomene
  • 84,125
  • 8
  • 85
  • 148
James Rowe
  • 33
  • 6
  • You need to learn some JavaScript basics, like how to write a function and return values from it. – melpomene May 27 '18 at 19:06
  • Using a function is the appropriate solution, you just need to use valid syntax and not forget the `return` keyword: `volume:function() { return Math.pow(this.radius,3)*4*Math.PI/6; }` – Bergi May 27 '18 at 19:14

0 Answers0