kobar1990 kobar1990 - 1 month ago 8
Javascript Question

Javascript calculation is off ( ^ operator )

I hope im asking this in the correct way, and ive honestly tried to search the web and specialy stackoverflow to see if its either a weird operator im screwing up in or if there where people with this problem but i cant seem to find any related problems and im lost on where i screw up. with that said:

Oke so i am trying to achieve this sum trough a variable in javascript, when i enter this sum into google, it provides me with the correct answer (wich is 41,1)

this sum --> ((2500/4500)^1.5)*100 = 41.4

yet in javascript (prob because the ^ is a binary XOR operator?) the answer will be 100.

So when i create a javascript var like:

<!DOCTYPE html>
<html>
<body>
<p>Calculate</p>

<button onclick="calculate()">calculate</button>

<div id="calcDiv"></div>

<script>
function calculate() {
var answer = ((2500/4500)^1.5)*100;
document.getElementById("calcDiv").innerHTML = answer;
}
</script>

</body>
</html>


when i would run this, the output is gonna be 100 (2500/4500)^1.5 will return 1 * 100 = 100.

Does anyone know how i can achieve the correct sum and output (i think by bypassing ^ or so, or there should be a other operator i dont know about?)

Answer

You can use the Math.pow(); method from JavaScript.

function calculate() {
    var answer = Math.pow(2500/4500, 1.5)*100;
    document.getElementById("calcDiv").innerHTML = answer;
}
<!DOCTYPE html>
<html>
<body>
<p>Calculate</p>

<button onclick="calculate()">calculate</button>

<div id="calcDiv"></div>
  
</body>
</html>