Thursday, May 26, 2016

a/A Practice Problem: Factorial

Challenge Description: "Write a method that takes an integer 'n' in; it should return
n*(n-1)*(n-2)*...*2*1. Assume n >= 0. As a special case, factorial(0) == 1"

Quick refresher on factorials: say n = 5, a factorial would be the product of 5*4*3*2*1 or 120.

Step 1: Write out your plan of action:

                         >> make function with one parameter
                         >> declare variable and set equal to 1
                         >> for loop to take each number less than n and multiply to variable
                         >> if statement for if n = 0
                         >> return product

Step 2: Declare function. Same rules for naming variables.

                                                  function factorial(n) {
                                                  }

Step 3: Set variable and set equal to 1.

                                                   function factorial(n) {
                                                      var product = 1;
                                                   }

Step 4: For loop with descending counter: Set i equal to n because you want to make sure it's the first number to start with and is included. The loop should stop once it hits one, or when i is greater than 0. Decrement by one each time to include each number below n (and over 0).

                                                    function factorial(n) {
                                                      var product = 1;
                                                      for (var i = n; i > 0; i--) {
                                                      }
                                                    }

Step 5: If statement for if n is 0; else multiply i to the variable product. The loop will start with n and every time it will be multiplied to the variable product.

                                                     function factorial(n) {
                                                       var product = 1;
                                                       for (var i = n; i < 0; i--) {
                                                           if (n === 0) {
                                                              product = 1;
                                                           } else {
                                                              product *= i;
                                                           }
                                                       }
                                                     }

Step 6: Return the variable product: because you have to return the answer...

                                                     function factorial(n) {
                                                        var product = 1;
                                                        for (var i = n; i < 0; i--) {
                                                           if (n === 0) {
                                                              product = 1;
                                                           } else {
                                                              product *= i;
                                                           }
                                                        }
                                                        return product;
                                                     }

There you have it! Any comments or questions are all appreciated.

Check out past problems here:

No comments :

Post a Comment