Hi y’all,
On the Learn TDD with Mocha: Factorial Feature (https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-test-driven-development-with-javascript/modules/fecp-learn-tdd-with-mocha/projects/factorial) project, task 6 says as follows:
Refactor your implementation code so that it is more concise and efficient. This could include cleaning up the existing code, or using a built-in method instead of a
for
loop for calculating the factorial of any input.
I was wondering what might be meant by using a built-in method there? I had a look with Google and can’t find anything that’s really more concise than a for loop—someone somewhere suggested a while loop, but you end up with more lines of code once you’ve declared the relevant variables. Any thoughts? My final code is below:
const Calculate = {
factorial(input){
if (input <= 1){
return 1;
};
let currentSum = input;
for (let i = input-1; i > 1; i--){
currentSum *= i;
};
return currentSum;
}
};