0

Por que el resultado es 2 ? / Why the result is 2 ?

function calc ( i ) { return ( i + i ) % 3; } for ( var i = 1; i < 4; ++i ) {} console.log ( calc ( i ) ); salida / output = 2

13th Dec 2020, 9:44 PM
Daniel Galetto
Daniel Galetto - avatar
1 Réponse
+ 6
The for-loop body is empty, so it only increases value of <i>. By the time the for-loop is done, value of <i> would be 4. Inside `calc` function, argument <i> is 4 so ( 4 + 4 ) % 3 8 % 3 And there you have 2, as a remainder of dividing 8 by 3.
14th Dec 2020, 12:54 AM
Ipang