Skip to main content
added 549 characters in body; added 6 characters in body; added 8 characters in body
Source Link
nick zoum
  • 6.7k
  • 7
  • 43
  • 92

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value, 3);

Unfortunately this will return a string and not a number, so to convert it to a number, you could either wrap it in a parseInt() or force it to turn into a number (i.e. subtracting it by 0).

var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);

Or

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value - 0, 3);

On a side-note, you should probably add the EventListener on the button and not on the span. I'm assuming you did not do that because the whole thing is in a form and thus it was redirecting the page. You can avoid that by adding a event.preventDefault();

var button = document.querySelector("#radius+button");

button.addEventListener("click", function(event){
    var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);
    volsphere.textContent = spherevolume;
    event.preventDefault();
});

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value, 3);

Unfortunately this will return a string and not a number, so to convert it to a number, you could either wrap it in a parseInt() or force it to turn into a number (i.e. subtracting it by 0).

var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);

Or

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value - 0, 3);

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value, 3);

Unfortunately this will return a string and not a number, so to convert it to a number, you could either wrap it in a parseInt() or force it to turn into a number (i.e. subtracting it by 0).

var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);

Or

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value - 0, 3);

On a side-note, you should probably add the EventListener on the button and not on the span. I'm assuming you did not do that because the whole thing is in a form and thus it was redirecting the page. You can avoid that by adding a event.preventDefault();

var button = document.querySelector("#radius+button");

button.addEventListener("click", function(event){
    var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);
    volsphere.textContent = spherevolume;
    event.preventDefault();
});
added 361 characters in body
Source Link
nick zoum
  • 6.7k
  • 7
  • 43
  • 92

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value, 3);

Unfortunately this will return a string and not a number, so to convert it to a number, you could either wrap it in a parseInt() or force it to turn into a number (i.e. subtracting it by 0).

var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);

Or

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value - 0, 3);

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value,3);

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value, 3);

Unfortunately this will return a string and not a number, so to convert it to a number, you could either wrap it in a parseInt() or force it to turn into a number (i.e. subtracting it by 0).

var spherevolume = (4/3)  * Math.PI * Math.pow(parseInt(radius.value), 3);

Or

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value - 0, 3);
Source Link
nick zoum
  • 6.7k
  • 7
  • 43
  • 92

The problem is that you are multiplying the HTMLElement it self and not its value. So add .value after radius, like so:

var spherevolume = (4/3)  * Math.PI * Math.pow(radius.value,3);