Reputation: 63
I need to write a program that takes two integers base and exponents and compute the exponents without using Math.Pow(). I have created the code using the Math.pow() method I can't figure out how to make it work without it. I have tried base^exp but it doesn't give me the right answer. Thanks in advance!
/* Write a JavaScript function named intPow that reads two numbers named base and exp from two text fields. Assume that the second number will always be an integer greater than or equal to 1. Your function should not use any of the built in Math functions such as Math.pow. Your function should use a loop to compute the value of baseexp meaning base raised to the power of exp. Your function must output the result of baseexp to a div. Hint: write your function to compute 1 multiplied by base exp times. */
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Integer Power</title>
<script type="text/javascript">
/* Write a JavaScript function named intPow that reads two numbers named base and exp from two text fields. Assume that the second number will always be an integer greater than or equal to 1. Your function should not use any of the built in Math functions such as Math.pow. Your function should use a loop to compute the value of baseexp meaning base raised to the power of exp. Your function must output the result of baseexp to a div. Hint: write your function to compute 1 multiplied by base exp times. */
function intPow() {
var base = parseFloat(document.getElementById("baseBox").value);
var exp = parseFloat(document.getElementById("expBox").value);
var output = "";
var i = 0;
for (i = 1; i <= exp; i++) {
output = Math.pow(base, exp);
}
document.getElementById("outputDiv").innerHTML = output;
}
</script>
</head>
<body>
<h1>Find the power of <i>Base</i> by entering an integer in the <i>base</i> box, and an integer in the <i>exponent</i> box.</h1> Base:
<input type="text" id="baseBox" size="15"> Exponents:
<input type="text" id="expBox" size="15">
<button type="button" onclick="intPow()">Compute Exponents</button>
<div id="outputDiv"></div>
</body>
</html>`
Upvotes: 0
Views: 13574
Reputation: 6533
with ES2016 you can use the exponentiation operator.
2 ** 8 // 256
Upvotes: 4
Reputation: 181
For anyone looking this up in the future, such as me just now, this is a solid solution:
function computePower(num, exponent) {
var result = 1;
for (i = 0; i < exponent; i++) {
result *= num;
}
return result;
}
Given a number and an exponent, "computePower" returns the given number, raised to the given exponent.
@user5500799,
output = (1 * base ) * exp;
won't work, because you are not raising the base to the exponent, just multiplying it. Starting with a multiplication with 1 is great though: in my code, that ensures that, for example, 2 to the power of 0 is 1 (everything to the power of 0 is one, which is something that is defined)
Upvotes: 5
Reputation: 63
I was able to figure it out by changint output to:
output = (1 * base ) * exp;
Upvotes: 0