Guest User

Untitled

a guest
Jul 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. const factorialize = (num) => {
  2. let prevValue = num; /*declare a variable to hold the previous num
  3. which has been assigned from our argument;*/
  4.  
  5. if (num === 0) return 1; /* evaluate an if statement to determine if num
  6. is equal to zero and if so return the value one;*/
  7.  
  8. while (num > 1) { /*the con condition is as long as our argument num
  9. is greater than one go through the statement*/
  10. num--; /*in the statement we start by decrementing our value
  11. of num until it get to one*/
  12.  
  13. prevValue *= num; /*
  14. now to our statement
  15. at the beginning we had set prevValue to num, beacuse in this
  16. example we begin at 5 our prevValue = 5 a the start of the statement.
  17.  
  18. 5 * 4 = 20 1st execution 5 is the prevValue and 4
  19. is the new decremented number after num--;
  20. 20 * 3 = 60 2nd exectuion 20 is now appended to prevValue
  21. and 3 is the new num fater num--;
  22. 60 * 2 = 120 3rd execution 60 is now appended to prevValue
  23. and 2 is the new num after num--;
  24. 120 * 1 = 120 4th execution 120 is now appended to prevValue
  25. and 1 is the new num after num--;
  26. and the statement is terminated because our condition has been met
  27. */
  28. }
  29. return prevValue; /* since our prevValue has an appended value of 120
  30. when it terminated because our condition had been met it returns that value.
  31. */
  32. }
  33. factorialize(5);
Add Comment
Please, Sign In to add comment