+ 6
I dont understand this
var x = 326; var prod = 1; (((what mean prod?))) while(x>0) { prod *= x%10; (((326%10 i am right?))) x = (x-x%10)/10; (((what mean this? 326 = (326-326%10)/10?))) } document.write(prod); answer: 36 (((why answer 36)))
3 ответов
+ 10
The algorithm computes the product (prod) of all digits in the input.
Input: 326
Output: 3 x 2 x 6 = 36
You're being confused about the difference between assignment and comparision. The = operator assigns the value from the right expression to the variable on the left.
The purposes of all statements in the loop are as follows:
1. prod *= x % 10; This statement multiplies prod with the current digit in the ones place - the first digit from right to left.
2. x = (x - x % 10) / 10; This statement trims the current digit in the ones place. 326 becomes 32, then 3 in the 2nd iteration, then 0 - loop terminated.
It's shown more clearly here:
https://code.sololearn.com/WDiY7zIX0op3/#js
+ 3
For my understandings:
% is modules operator
10%3=1
var x = 326;
var prod = 1; (((Global variable prod)))
while(x>0) {
prod *= x%10; (((1*326%10=6)))
x = (x-x%10)/10; (((then im lost...)))
}
document.write(prod);
- 6
do the javascripr lesson
.then you will understand