By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

Recursive "Divide" Function gives wrong results

P: 35
there is something wrong with my code. I was doing some homework trying to implement the "divide" function recursively in JS but it gives some wrong results. here's my code:

it returns the quotient and remainder.
Expand|Select|Wrap|Line Numbers
  1. function Divide(x,y)
  2.     {
  3.         var result1;
  4.         if (x == 0)
  5.             return [0,0];
  6.         result = Divide(Math.floor(x/2),y);
  7.         q = result[0];
  8.         r = result[1];
  9.         q = 2*q;
  10.         r = 2*r;
  11.         if ((x % 2) == 1)  // if x is odd
  12.         {
  13.             r = r + 1;
  14.         }
  15.         if (r >= y)
  16.         {
  17.             r = r - 1;
  18.             q = q + 1;
  19.         }
  20.         return [q,r];
  21.     }
  22.  
I know you guys know how to do this. thanks again!:D
Aug 18 '09 #1
Share this Question
Share on Google+
1 Reply


P: 35
nevermind. erase this..xD i got it now. sheesh..xD
Aug 18 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.