Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const factorialize = (num) => {
- let prevValue = num; /*declare a variable to hold the previous num
- which has been assigned from our argument;*/
- if (num === 0) return 1; /* evaluate an if statement to determine if num
- is equal to zero and if so return the value one;*/
- while (num > 1) { /*the con condition is as long as our argument num
- is greater than one go through the statement*/
- num--; /*in the statement we start by decrementing our value
- of num until it get to one*/
- prevValue *= num; /*
- now to our statement
- at the beginning we had set prevValue to num, beacuse in this
- example we begin at 5 our prevValue = 5 a the start of the statement.
- 5 * 4 = 20 1st execution 5 is the prevValue and 4
- is the new decremented number after num--;
- 20 * 3 = 60 2nd exectuion 20 is now appended to prevValue
- and 3 is the new num fater num--;
- 60 * 2 = 120 3rd execution 60 is now appended to prevValue
- and 2 is the new num after num--;
- 120 * 1 = 120 4th execution 120 is now appended to prevValue
- and 1 is the new num after num--;
- and the statement is terminated because our condition has been met
- */
- }
- return prevValue; /* since our prevValue has an appended value of 120
- when it terminated because our condition had been met it returns that value.
- */
- }
- factorialize(5);
Add Comment
Please, Sign In to add comment