0

Cannot get this program to return the average of 3 numbers

function main() { var num1 = parseInt(readLine(),10); var num2 = parseInt(readLine(),10); var num3 = parseInt(readLine(),10); var average; //assign the average value to the variable average avg(num1, num2, num3); console.log(average); } //complete the function function avg(num1, num2, num3) { average = (num1 + num2 + num3) /3; return average; }

13th Jul 2021, 9:33 PM
Natanael
4 Answers
+ 1
And also dont forget to call main function with main()
13th Jul 2021, 10:54 PM
PanicS
PanicS - avatar
0
Thank you, I thought I was calling the function here with avg=(num1,num2,num3); and I guess I was but as you said without assigning the value to "average" I guess outside of the function avg, the variable average loses scope. I think if I had console.log(average) within function avg it would have worked
15th Jul 2021, 7:15 PM
Natanael
0
I just tried it without assigning a return value at all and it worked but of course part of this task is to return a value function main() { var num1 = parseInt(readLine(),10); var num2 = parseInt(readLine(),10); var num3 = parseInt(readLine(),10); var average; //assign the average value to the variable average avg(num1, num2, num3); //console.log(average); } //complete the function function avg(num1, num2, num3) { average = (num1 + num2 + num3) /3; console.log(average); }
15th Jul 2021, 7:20 PM
Natanael