|
| 1 | +--- |
| 2 | +id: 66c07238b01053abaf812065 |
| 3 | +title: Build a Factorial Calculator |
| 4 | +challengeType: 14 |
| 5 | +dashedName: build-a-factorial-calculator |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +A factorial is the product of an integer and all the integers below it. For example, the factorial of `5` is `5 * 4 * 3 * 2 * 1 = 120`. In this lab, you will create a factorial calculator that takes a number from the user and calculates the factorial of that number. |
| 11 | + |
| 12 | +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. |
| 13 | + |
| 14 | +**User Stories:** |
| 15 | + |
| 16 | +1. You should declare a variable `num` and assign it a number of your choice. The assigned number should be between `1` and `20` inclusive. |
| 17 | +1. Create a function named `factorialCalculator` that takes a number as an argument and returns the factorial of that number. |
| 18 | +1. Inside the function, declare a `result` variable and assign it the value of `1`. Using a loop, loop through all numbers from `1` to the input number(inclusive) and for each number, multiply the `result` variable by the current number and assign the result to the `result` variable. You can choose to use either a `for` loop, `while` loop or `do...while` loop here. |
| 19 | +1. You should call the `factorialCalculator` function with with `num` as the argument and assign the result to the variable `factorial`. |
| 20 | +1. You should store the final output in the format `Factorial of [num] is [factorial]` and assign it to the variable `resultMsg`. |
| 21 | +1. You should output the value of `resultMsg` to the console. |
| 22 | + |
| 23 | +# --hints-- |
| 24 | + |
| 25 | +You should have a `num` variable. |
| 26 | + |
| 27 | +```js |
| 28 | +assert.isDefined(num); |
| 29 | +``` |
| 30 | + |
| 31 | +You should assign a value to the `num` variable |
| 32 | + |
| 33 | +```js |
| 34 | +assert.isNotNull(num); |
| 35 | +``` |
| 36 | + |
| 37 | +The value of `num` should be between `1` and `20`. |
| 38 | + |
| 39 | +```js |
| 40 | +assert.isAtLeast(num, 1); |
| 41 | +assert.isAtMost(num, 20); |
| 42 | +``` |
| 43 | + |
| 44 | +The `num` value should be a number. |
| 45 | + |
| 46 | +```js |
| 47 | +assert.isNumber(num); |
| 48 | +``` |
| 49 | + |
| 50 | +You should have a function named `factorialCalculator`. |
| 51 | + |
| 52 | +```js |
| 53 | +assert.isFunction(factorialCalculator); |
| 54 | +``` |
| 55 | + |
| 56 | +The `factorialCalculator` function should take a number as an argument. |
| 57 | + |
| 58 | +```js |
| 59 | +assert.match(factorialCalculator.toString(), /\s*factorialCalculator\(\s*\w+\s*\)/); |
| 60 | +``` |
| 61 | + |
| 62 | +The factorial of `5` should return `120`. |
| 63 | + |
| 64 | +```js |
| 65 | +assert.strictEqual(factorialCalculator(5), 120); |
| 66 | +``` |
| 67 | + |
| 68 | +The factorial of `7` should return `5040`. |
| 69 | + |
| 70 | +```js |
| 71 | +assert.strictEqual(factorialCalculator(7), 5040); |
| 72 | +``` |
| 73 | + |
| 74 | +For `5`, the `resultMsg` should contain `Factorial of 5 is 120`. |
| 75 | + |
| 76 | +```js |
| 77 | +assert.strictEqual(resultMsg, 'Factorial of 5 is 120'); |
| 78 | +``` |
| 79 | + |
| 80 | +You should call your `factorialCalculator` function with the `num` variable as the argument. |
| 81 | + |
| 82 | +```js |
| 83 | +assert.match(__helpers.removeJSComments(code), /factorialCalculator\(\s*num\s*\)\s*;?\s?$/m); |
| 84 | +``` |
| 85 | + |
| 86 | +You should define a `factorial` variable and assign the result of the `factorialCalculator` function to it. |
| 87 | + |
| 88 | +```js |
| 89 | +assert.isDefined(factorial); |
| 90 | +assert.strictEqual(factorial, factorialCalculator(num)); |
| 91 | +``` |
| 92 | + |
| 93 | +Your `factorialCalculator` should produce the correct result. |
| 94 | + |
| 95 | +```js |
| 96 | +assert.strictEqual(factorialCalculator(6), 720); |
| 97 | +assert.strictEqual(factorialCalculator(9), 362880); |
| 98 | +assert.strictEqual(factorialCalculator(11), 39916800); |
| 99 | +``` |
| 100 | + |
| 101 | +You should output the value of `resultMsg` to the console. |
| 102 | + |
| 103 | +```js |
| 104 | +assert.match(__helpers.removeJSComments(code), /console\.log\(resultMsg\)\s*;?\s?$/m); |
| 105 | +``` |
| 106 | + |
| 107 | +# --seed-- |
| 108 | + |
| 109 | +## --seed-contents-- |
| 110 | + |
| 111 | +```js |
| 112 | + |
| 113 | +``` |
| 114 | + |
| 115 | +# --solutions-- |
| 116 | + |
| 117 | +```js |
| 118 | +const num = 5; |
| 119 | + |
| 120 | +function factorialCalculator(n) { |
| 121 | + let result = 1; |
| 122 | + |
| 123 | + for (let i = 2; i <= n; i++) { |
| 124 | + result *= i; |
| 125 | + } |
| 126 | + |
| 127 | + return result; |
| 128 | +} |
| 129 | + |
| 130 | +const factorial = factorialCalculator(num); |
| 131 | +const resultMsg = `Factorial of ${num} is ${factorial}`; |
| 132 | +console.log(resultMsg); |
| 133 | +``` |
0 commit comments