Iterative factorial Finish the provided factorial function s

Iterative factorial: Finish the provided factorial function, so that it returns the value n!. Your code should use a forloop to compute the product 1 * 2 * 3 * ... * n. If you write the code carefully, you won\'t need a special case for when n equals 0.

Once implemented, uncomment the Program.assertEqual() statements at the bottom to verify that the test assertions pass.

var factorial = function(n) {
var result = 1;
for(var i = ......; i <= ..... ; ......) {
.......;
}

return result;
};

println(\"The value of 5! should be \" + 5*4*3*2*1);
println(\"The value of 5! is \" + factorial(5));
println(\"The value of 0! should be 1\");
println(\"The value of 0! is \" + factorial(0));

//Program.assertEqual(factorial(5), 120);
//Program.assertEqual(factorial(0), 1);

Solution

function(n) {
var result = 1;
for(var i =1; i <=n ; i++) {
result=i*n;

}

Program.assertEqual(factorial(5), 120);
Program.assertEqual(factorial(0), 1);

println(\"The value of 5! should be \" + 5*4*3*2*1);
println(\"The value of 5! is \" + factorial(5));
println(\"The value of 0! should be 1\");
println(\"The value of 0! is \" + factorial(0));

Iterative factorial: Finish the provided factorial function, so that it returns the value n!. Your code should use a forloop to compute the product 1 * 2 * 3 *

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site