Been trying for the past few hours but can’t seem to make it work.
I need to calculate the square root of a sum. There seems to exist a javascript method to calculate this (http://www.w3schools.com/jsref/jsref_sqrt.asp) using Math.sqrt(). How can I use this method so that it calculates the square root of my sum? The sum is displayed after you fill in the two input fields and works fine..
DEMO or code below:
$(document).ready(function(){
$('#beers').keyup(calculate);
$('#ranking').keyup(calculate);
function calculate(e)
{
$('p#sum').html($('#beers').val() * 5 / $('#ranking').val() + 1); //calculation of the two fields
/*Math.sqrt()*/ //This is the function to calculate the square root
}
});
What you need to do is call
Math.sqrtby passing in the number that you want the square-root of: http://jsfiddle.net/zLKgT/2/You don’t need to convert anything to a number before hand because you are using number objects and not using the
+operator which functions both for addition and concatenation. Javascript will automatically convert thevals to numbers.