+ 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 Answer
+ 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.