**New course!**Every coder should learn

**Generative AI!**

0

# Factorial fun challenge on JS within conditional loops

I am familiar with conditional loops but I'm having a hard time figuring this out, I understand what a factorial is 5x4x3x2x1 = 120 but can't figure out the code to make it work. This is within JS conditional loops

12 Answers

+ 2

Hint : use reverse for loop and multiply each Iterable value with a temporary variable which hold value 1. Every time this temporary value will change like if loop start from 5 to till 1 then temporary value will be like 5 * 4 * 3 * 2 * 1. Remember here loop should be terminate when it comes to 0 and it should avoid to multiply with 0 otherwise you will get 0.

+ 2

initialize a result variable to 1
loop for i==2 to i==n (n is the number you want the factorial) by step of 1, or while n>1 decrementing n at end of each iteration, and inside loop body (at eaxh iteration) multiply result by i (or n in case of while loop)...
at end, result hold !n result ;)

+ 2

since you do nothing with the incremented variable, pre or post increment doesn't matter and both are equivalent...
however, if you assign the incremented variable to another one (or use it inside an expression), result should vary:
var a = 42;
var b = a++; // b == 42, a == 43
but:
var a = 42;
var b = ++a; // b == 43, a == 43
and so:
var a = 42;
var b = 1 + ++a + 2; // 46
but:
var a = 42;
var b = 1 + a++ + 2; // 45
In your loop, ++temp or temp++ are equivalent and produce exact same result...
with 3 as input
iteration 1: temp == 2 <= 3
iteration 2: temp == 3 <= 3
iteration 3: temp == 4 > 3 so exit before use...
incrementation (third) part of loop occurs at end of each iteration
test (second) part of loop occurs at start of each iteration
initialization (first) part of loop occurs only once, just before starting loop...

0

Thanks so much, I will continue to try this and use a reverse for loop as you said and also use a variable to hold the first multiplication result 5x4 = 20
This is a tough one for me because I've never seen anything like this. Thanks

0

/* Here's my code I'm totally lost because I don't know how to make the variable which holds the first multiplication 5x4 hold other numbers correctly other than 20. It's hard to see but I don't know how to insert code here */
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
var temp = number;
var answer = 0;
// Your code here
for (; number >= factorial; number--)
{
// var answer will hold 20 in case of 5x4
answer = temp * (number-1);
console.log(answer);
}
}

0

function main() {
var number = parseInt(readLine(),10);
var factorial = 1;
for (var temp=2; temp<=number; ++temp) {
factorial *= temp;
}
console.log(factorial);
}
// or:
function main() {
var number = parseInt(readLine(),10);
var factorial = 1;
while (2<=number) {
factorial *= number;
number--;
}
console.log(factorial);
}
// or:
function main() {
var number = parseInt(readLine(),10);
var factorial = 1;
for (; 2<=number; --temp) {
factorial *= number;
}
console.log(factorial);
}

0

Thank you, I would have never figured this out on my own, I knew there had to be a variable that would hold the values but it looks like I was trying to print all the numbers from
1 to (var number) without adding to factorial=factorial*temp.
Thanks
My favorite code was:
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
// Your code here
for ( var temp=2; temp<=number; ++temp)
{
factorial *= temp;
}
console.log(factorial)
}

0

Hi, I'm trying to solve this JS project but I'm getting a NaN error message.
I'm trying to add 5 each day within the loop to solve this challenge and on the last one add 7.
Here's my code, I don't know why I'm getting a not a number error message NaN
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var distance = 0;
var days = 1;
var t;
for (; days <= depth; days++)
{
t+=5; // add 5 to t each day
if (days == depth )
{
break;
}
}
console.log(days);
t+=7; // getting a NaN error here
console.log(t);
}

0

you must initialize t variable to zero... and you must post new questions in new threads ^^

0

Ok thank you

0

Message for visph about factorial code.
Please look at the code below, I am curious because it's strange how this code works whether you increment the temp variable in this order ++temp or like this temp++.
As I understand it ++temp adds 1 to temp right away so if you follow the sequence adding with 3 as input you will see that ++temp should not work, it seems like it should only work by implementing temp this way temp++.
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
// Your code here
for ( var temp=2; temp<=number; ++temp)
{
factorial *= temp;
}
console.log(factorial)
}

0

Thank you so much, I thought that if I increment ++temp at the third part of the loop this variable would be incremented immediately, and this way temp++ would increment it at the end of the iteration before the new iteration.
So I guess if it is incremented right before the new iteration it doesn't matter whether it's ++temp or temp++, but I think that you see what I was trying to say that ++temp incremented right at the third part of the loop would mess things up when the loop gets to this variable: factorial *= temp;