-1

This is the input box part (HTML). Definition of the function and my currently unfilled for loop.

<input type="number" id="num1"><!--Fill in the number-->
<input type="number" id="num2"><!--Fill in the power-->
<button onClick="multByItself()">Enter</button>

<script>
function multByItself(){
  var num1 = document.getElementById("num1");
  var num2 = document.getElementById("num2");
  for(var i = 0; i < num2; i++){
    // I am unsure what to fill in here
  }
}
</script>
jo3rn
  • 1,291
  • 1
  • 11
  • 28
  • Does this answer your question? [Getting DOM element value using pure JavaScript](https://stackoverflow.com/questions/4173391/getting-dom-element-value-using-pure-javascript) – Tân Apr 07 '20 at 07:20

2 Answers2

2

You can simply use the Math.pow function which does exactly what you want without using any looping. A for loop is straight overkill for your problem.

function multByItself(){
  let num = document.getElementById("num1").value;
  let pow = document.getElementById("num2").value;
  console.log(Math.pow(num, pow));
}
<input type="number" id="num1"><!--Fill in the number-->
<input type="number" id="num2"><!--Fill in the power-->
<button onClick="multByItself()">Enter</button>
Aaron
  • 1,600
  • 1
  • 8
  • 14
2

If you want to use JavaScript for loop, then you can go for this approach.

var answer = 1;
for(var i = 0; i < num2; i++) {
    answer = answer * num1;
}
console.log(answer);
Sumesh Sivan
  • 193
  • 7