+ 1

[Loop] [JavaScript] Factorial Fun Quesions

Hi everyone, I am troubled by this problem, please advice, many thanks😊 ————————- MY CODE function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here for (vat i=0; i <=number; i++){ number * (number-i) } console.log(number) } ——————— 1. When two variables are set, which one is the var inside the For loop? 2. Ithought a “factorial” is the PRODUCT of all positive integers less than or equal to the number. Why set the variable to 1?

8th Oct 2021, 12:30 AM
Liangyong Peng
Liangyong Peng - avatar
9 ответов
+ 4
So factorial 5 = 1×2×3×4×5 Factorial 0 is 1 Your code needs to sum in the loop which number*(number-i) does not do in any language I am aware of 😉 Something like factorial *= i... starting factorial at 1 overcomes the issue of returning 0 as the sum. 0x1x2x3x4x5=0
8th Oct 2021, 1:34 AM
Paul K Sadler
Paul K Sadler - avatar
+ 3
Please always tag the programming language. https://code.sololearn.com/W3uiji9X28C1/?ref=app
8th Oct 2021, 1:01 AM
Simon Sauter
Simon Sauter - avatar
+ 2
In the for loop you have a typo. vat i=0 should be var=1
8th Oct 2021, 1:24 AM
Paul K Sadler
Paul K Sadler - avatar
+ 2
As to your questions: 1. The variable number holds the number for which you should calculate the factorial. It is the upper limit for the loop and determines the number of iterations. The variable factorial holds the result of each step of the calculation which after the last iteration of the loop is the result that you should output. 2. The value of factorial changes inside the loop. 1 is only the initial value, at the end the value of factorial is the factorial of number. So here's what the program should do: Take a number as input and assign it to the variable number. Initialise the variable factorial setting it to 1. Loop through the numbers 1 to number. Inside the loop multiply the variable factorial by the loop variable. After the last Iteration output the variable factorial.
8th Oct 2021, 2:00 AM
Simon Sauter
Simon Sauter - avatar
+ 2
So in your second example let's work through it... number = 5 factorial = 1 Loop... i =1 Pass 1 i = 1, number = 5, factorial = 1 x 5 = 5 Pass 2 i = 1, number = 4, factorial = 5 x 4 = 20 Pass 3 i = 1, number = 3, factorial = 3 x 20 = 60 Pass 4 i = 1, number = 2, factorial = 2 x 60 = 120 Pass 5 i<number is false loop terminates
8th Oct 2021, 12:36 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Hi Simon Sauter, Have changed the title, your answer is very knowledgeable and helpful, thanks! Hi Paul K Sadler, fixed the typo and got the idea of why starting factorial at 1, thank you!😄 ------------------------------------------- Both solutions work: 1. function main() { var number = parseInt(readLine(), 10) var factorial = 1; // Your code here for ( i=1;i<= number; i++) { factorial *= i; } console.log (factorial) } 2. function main() { var number = parseInt(readLine(), 10) var factorial = 1; //Your code here for ( i= 1; i < number; number--) { factorial*=number; } console.log (factorial); } ----------------------------------------- But I have a further question now, In the SOLUTION 2, why the statement 2 could be i=number or i<=number? They both work, but I don't get the logic behind it. -------------------------------------------------- And I've created a note for this question based on your answers, hope this can help people with similar problem. please visit https://hackmd.io/@G0jOQteCSSeB-_fWD9vMgw/SJrqEvTNY
8th Oct 2021, 6:56 AM
Liangyong Peng
Liangyong Peng - avatar
+ 1
Hi Paul, Thank you for your patience and careful explanation, as a beginner with a passion for programming, I understand the logic behind it better through your explanation!😊
13th Oct 2021, 1:12 PM
Liangyong Peng
Liangyong Peng - avatar
0
OH, ok! But when I tried to add a new tag by editing my post, the system sent me back an error message. What should I do? Re-post the question?
8th Oct 2021, 1:24 AM
Liangyong Peng
Liangyong Peng - avatar
0
Liangyong Peng if you can't add a tag just add the language to the title.
8th Oct 2021, 1:26 AM
Simon Sauter
Simon Sauter - avatar