+ 1

Why does the the first way work but not the second?

function main() { var num1 = parseInt(readLine(),10); var num2 = parseInt(readLine(),10); var num3 = parseInt(readLine(),10); var average = (avg(num1,num2,num3 ) / 3) console.log(average) function avg(n1,n2,n3){ return n1 + n2 + n3 } Vs function main() { var num1 = parseInt(readLine(),10); var num2 = parseInt(readLine(),10); var num3 = parseInt(readLine(),10); var average = (avg(num1,num2,num3 )) console.log(average) } function avg(n1,n2,n3){ return n1 + n2 + n3 / 3 }

1st Mar 2021, 1:42 PM
SuperKitsune94
SuperKitsune94 - avatar
2 ответов
+ 2
In 2nd way, you have to use return (n1+n2+n3)/3
1st Mar 2021, 1:56 PM
Jayakrishna 🇮🇳
0
2nd doesn't works because: n1+n2+n3/3 == n1+n2+(n3/3) != (n1+n2+n3)/3
1st Mar 2021, 2:04 PM
visph
visph - avatar