1

The question which is suggested it is a question which is based on calculating fractional power of number but I wanted it simply in integer form.The function power is showing the powers correctly of a number raised to power b but it is not stopping as expected it should be.When I try to return the sum as return sum at the end of function power it is just loading and showing nothing please help me.Any help will be greatly appreciated.I cant use the built in pow() function.Thanks.

function power(a, b) {
  a = parseInt(a);
  b = parseInt(b);
  var sum = 1;
  var result;
  var pow = 1;
  for (var i = 1; i <= b; i++) {
    pow = 1;

    if (i == 1) {
      pow = a * i;
      sum = 1;
    } else {
      i--;
      pow = a * i;
      sum = sum * pow;
      alert(sum);
    }

  }

}
var a = prompt("Enter the number a for calculating its power?");
var b = prompt("Enter the number for calculating pow of a i.e enter power of a");

var answer = power(a, b);
alert("a^b is equal to : " + answer);
bilalmohib
  • 280
  • 3
  • 16
  • Why are you re-inventing the wheel? [`Math.pow()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow) – Liam Jul 15 '20 at 08:04
  • @Liam Probably an assignment – Alon Eitan Jul 15 '20 at 08:04
  • Lol, what a pointless thing to do :) I love how lazy academics just come up with this stuff that'll you'll never do in real life. Just do `power(a,b){ return Math.pow(a,b);}` A* – Liam Jul 15 '20 at 08:05
  • @ Alon Eitan I have been assigned this work and I have to do this in my custom function without the built in function Its my javascript course one of assignments – bilalmohib Jul 15 '20 at 08:05
  • @Liam Man I have to do it myself without the built in function – bilalmohib Jul 15 '20 at 08:06
  • Just saying https://stackoverflow.com/a/23987676/542251 – Liam Jul 15 '20 at 08:07
  • Your actual issue seems to be you never `return pow` BTW. Though I'm also not sure your function actually calculates pow either – Liam Jul 15 '20 at 08:08
  • 2
    At the start of the loop `i` equals to 0, then you decrement it by one, on the next round `i` is 0 again, and you decrement it by one etc. What is the purpose of `i--`? – Teemu Jul 15 '20 at 08:10
  • @Liam Its a very difficult algorithm I want it in a simple way – bilalmohib Jul 15 '20 at 08:10
  • 1
    You do `i++` and `--i` at each loop. The value of `i` is always 0, resulting in an infinite loop. – Jeremy Thille Jul 15 '20 at 08:12
  • @Teemu I decremented it because the power function didnt calculated the power correctly and it was calculating wrong now in alerts it is giving correct powers of number a raised to power b but bot loading just again and again incrementing if I return its not stopping just loading – bilalmohib Jul 15 '20 at 08:12
  • You should not touch `i` inside the loop that way, it always remains at zero, and the loop will never finish. You've to fix the logic to work without changing `i` inside the loop. – Teemu Jul 15 '20 at 08:15
  • Does this answer your question? [Writing your own exponential power function with decimals](https://stackoverflow.com/questions/23982504/writing-your-own-exponential-power-function-with-decimals) – Liam Jul 15 '20 at 08:24
  • @Sir I have got an answer but the suggested one involves fractions.Thanks – bilalmohib Jul 15 '20 at 08:28

1 Answers1

2

If you are interested in recursive function call, check this out.,

var power = function(a, b) 
{

a = parseInt(a);
b = parseInt(b);


   if (b === 0) 
   {
    return 1;
    }
  else 
  {
    return a * power(a, b-1);
  }
};

var a = prompt("Enter the number a for calculating its power?");
var b = prompt("Enter the number for calculating pow of a i.e enter power of a");



alert("the result "+  a + " ^ " + b + " is "  + power(a, b));
Santa
  • 367
  • 2
  • 8