What’s wrong with this JavaScript code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What’s wrong with this JavaScript code

It’s a practice question in sololearn javascript. Practice Using “return”. Question and my coding (After //complete the function) as follows and I am not sure what’s wrong with it. Question: You are given a program that takes 3 numbers as input. Complete the given function to calculate the average of those 3 numbers, assign it to the given variable, and output it. Sample Input 3 6 9 Sample Output 6 ............................................................ Coding: 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 console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/2; return average;} console.log(avg(num1,num2,num3));

27th Oct 2020, 10:23 PM
C Yam
C Yam - avatar
39 Answers
+ 9
updated (thanks it works now) really appreciate it : 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 average = avg(num1,num2,num3); console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/3; return average};
27th Oct 2020, 11:16 PM
C Yam
C Yam - avatar
+ 3
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 average = avg(num1, num2, num3); console.log(average) } //complete the function function avg(...args) { let result = args.reduce((acc, current) => acc + current); return result / args.length; }; This function is more common.
2nd Sep 2021, 12:52 PM
Алексей Саблин
Алексей Саблин - avatar
+ 1
the working solution... 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 average = avg(num1,num2,num3); console.log(average); } //complete the function function avg(num1,num2,num3){ return (num1+num2+num3)/3; }
26th Apr 2022, 5:18 PM
Amit Kumar Mishra
0
hi, you must divide with number of numbers. in this case is 3.
27th Oct 2020, 10:46 PM
Marouen Slimani
Marouen Slimani - avatar
0
i tried changing to the following: function avg(num1,num2,num3){ var average = (num1+num2+num3)/3; return average;} console.log(avg(num1,num2,num3)); the output says “num1 is not defined” i am confused as num1 is clearly defined.
27th Oct 2020, 10:50 PM
C Yam
C Yam - avatar
0
Cheri Yan num1 is defined inside function main() , Call avg(num1,...) function inside that main to access num1 ,num2 and num3 variable
27th Oct 2020, 10:58 PM
Abhay
Abhay - avatar
0
Where you adding console.log () statement? Try this.. Cheri Yan IN main function : .. var average = avg(num1, num2, num3) ; console.log(average) }
27th Oct 2020, 10:59 PM
Jayakrishna 🇮🇳
0
if i put var average in the main , as suggested. it becomes: 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 average = avg(num1,num2,num3); console.log(average) } //complete the function the output is: reference error again saying “avg” is not defined.
27th Oct 2020, 11:09 PM
C Yam
C Yam - avatar
0
It works without the second function and no return needed :) function main() { var num1 = parseInt(readLine(),10); var num2 = parseInt(readLine(),10); var num3 = parseInt(readLine(),10); var average = (num1 + num2 + num3) / 3; //assign the average value to the variable average console.log(average) }
9th Jun 2021, 12:46 PM
Igor
Igor - avatar
0
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 average = (num1 + num2 + num3) / 3 console.log(average) } //complete the function function avg(){ }
17th Jun 2021, 2:20 PM
Agon Istrefi
0
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 average = avg(num1,num2,num3)/3; return average } //complete the function function avg(num1 ,num2 ,num3){ console.log((num1 + num2 + num3)/3)
26th Jun 2021, 6:34 PM
Abiel M
Abiel M - avatar
0
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 average = avg(num1,num2,num3); console.log(average) } //complete the function function avg(num1,num2,num3){ var average = (num1 + num2 + num3)/3; return average; }
15th Nov 2021, 7:21 AM
Maleesha Kumarasinghe
Maleesha Kumarasinghe - avatar
0
updated (thanks it works now) really appreciate it : function main() { var num1 =updated (thanks it works now) really appreciate it : 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 average = avg(num1,num2,num3); console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/3; return average}; parseInt(readLine(),10); var num2 = parseInt(readLine(),10); var num3 = parseInt(readLine(),10); var average; //assign the average value to the variable average average = avg(num1,num2,num3); console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/3; return average};
15th Dec 2021, 12:48 AM
Marvin Manaog
Marvin Manaog - avatar
0
The given code takes 3 numbers as input. Complete the program to output the maximum of the 3 inputs.
15th Dec 2021, 2:05 AM
Marvin Manaog
Marvin Manaog - avatar
0
What’s wrong with this JavaScript code It’s a practice question in sololearn javascript. Practice Using “return”. Question and my coding (After //complete the function) as follows and I am not sure what’s wrong with it. Question: You are given a program that takes 3 numbers as input. Complete the given function to calculate the average of those 3 numbers, assign it to the given variable, and output it. Sample Input 3 6 9 Sample Output 6 ............................................................ Coding: 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 console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/2; return average;} console.log(avg(num1,num2,num3));
15th Dec 2021, 2:17 AM
Marvin Manaog
Marvin Manaog - avatar
15th Dec 2021, 2:20 AM
Marvin Manaog
Marvin Manaog - avatar
0
What’s wrong with this JavaScript code It’s a practice question in sololearn javascript. Practice Using “return”. Question and my coding (After //complete the function) as follows and I am not sure what’s wrong with it. Question: You are given a program that takes 3 numbers as input. Complete the given function to calculate the average of those 3 numbers, assign it to the given variable, and output it. Sample Input 3 6 9 Sample Output 6 ............................................................ Coding: 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 console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/2; return average;} console.log(avg(num1,num2,num3));
15th Dec 2021, 2:21 AM
Marvin Manaog
Marvin Manaog - avatar
0
What’s wrong with this JavaScript code What’s wrong with this JavaScript code It’s a practice question in solole It’s a practice question in sololearn javascript. Practice Using “return”. Question and my coding (After //complete the function) as follows and I am not sure what’s wrong with it. Question: You are given a program that takes 3 numbers as input. Complete the given function to calculate the average of those 3 numbers, assign it to the given variable, and output it. Sample Input 3 6 9 Sample Output 6 ............................................................ Coding: 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 console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/2; return average;} console.log(avg(num1,num2,num3));
15th Dec 2021, 2:28 AM
Marvin Manaog
Marvin Manaog - avatar
0
What’s wrong with this JavaScript code It’s a practice question in sololearn javascript. Practice Using “return”. Question and my coding (After //complete the function) as follows and I am not sure what’s wrong with it. Question: You are given a program that takes 3 numbers as input. Complete the given function to calculate the average of those 3 numbers, assign it to the given variable, and output it. Sample Input 3 6 9 Sample Output 6 ............................................................ Coding: 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 console.log(average) } //complete the function function avg(num1 ,num2 ,num3){ var average = (num1 + num2 + num3)/2; return average;} console.log(avg(num1,num2,num3));
15th Dec 2021, 2:29 AM
Marvin Manaog
Marvin Manaog - avatar
0
The given code takes 3 numbers as input. Complete the program to output the maximum of the 3 inputs.
15th Dec 2021, 2:54 AM
Marvin Manaog
Marvin Manaog - avatar