+ 2
JS challenge question
I didn't understand this question. If I call the function guess(c,b,a); output is 21, that is understandable but in case of guess(a,c,b); output is 25, means c takes b's place. how? please help me to understand this. var a=2,b=3,c=5; function guess(c,b,a){ console.log(b*(c+a)); } guess(c,b,a); //output 21 guess(a,c,b); //output 25
1 Resposta
+ 1
It’s about parameter order!
When you call a function, the parameters get assigned in the order you pass them.
Your function expects: guess(c, b, a)
• 1st parameter = c
• 2nd parameter = b
• 3rd parameter = a
For guess(c,b,a): You pass c=5, b=3, a=2
• So it calculates: b * (c + a) = 3 * (5 + 2) = 3 * 7 = 21 ✓
For guess(a,c,b): You pass a=2, c=5, b=3
• But the function still thinks: 1st param is “c”, 2nd is “b”, 3rd is “a”
• So it becomes: c=2, b=5, a=3
• Calculates: b * (c + a) = 5 * (2 + 3) = 5 * 5 = 25 ✓
The function doesn’t care what the original variable names were - it just uses whatever values you pass in the order you pass them.