curriculum/challenges/english/blocks/lab-factorial-calculator/66c07238b01053abaf812065.md
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.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
num and assign it a number of your choice. The assigned number should be between 1 and 20 inclusive.factorialCalculator that takes a number as an argument and returns the factorial of that number.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.factorialCalculator function with num as the argument and assign the result to the variable factorial.Factorial of [num] is [factorial] and assign it to the variable resultMsg.resultMsg to the console.You should have a num variable.
assert.isDefined(num);
You should assign a value to the num variable
assert.isNotNull(num);
The value of num should be between 1 and 20.
assert.isAtLeast(num, 1);
assert.isAtMost(num, 20);
The num value should be a number.
assert.isNumber(num);
You should have a function named factorialCalculator.
assert.isFunction(factorialCalculator);
The factorialCalculator function should take a number as an argument.
assert.match(factorialCalculator.toString(), /\s*factorialCalculator\(\s*\w+\s*\)/);
The factorial of 5 should return 120.
assert.strictEqual(factorialCalculator(5), 120);
The factorial of 7 should return 5040.
assert.strictEqual(factorialCalculator(7), 5040);
You should call your factorialCalculator function with the num variable as the argument.
assert.match(__helpers.removeJSComments(code), /factorialCalculator\(\s*num\s*\)\s*;?\s*$/m);
You should define a factorial variable and assign the result of the factorialCalculator function to it.
assert.isDefined(factorial);
assert.strictEqual(factorial, factorialCalculator(num));
Your factorialCalculator should produce the correct result.
assert.strictEqual(factorialCalculator(6), 720);
assert.strictEqual(factorialCalculator(9), 362880);
assert.strictEqual(factorialCalculator(11), 39916800);
Your resultMsg should contain a string in a format Factorial of [num] is [factorial].
assert.strictEqual(resultMsg, `Factorial of ${num} is ${factorialCalculator(num)}`);
assert.notMatch(__helpers.removeJSComments(code), /Factorial of \d{1,2} is \d+/);
You should output the value of resultMsg to the console.
assert.match(__helpers.removeJSComments(code), /console\.log\(resultMsg\)\s*;?\s?$/m);
const num = 5;
function factorialCalculator(n) {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
const factorial = factorialCalculator(num);
const resultMsg = `Factorial of ${num} is ${factorial}`;
console.log(resultMsg);