0
[Average of three]The return statement issue
Hi 👋, Some questions, pls advice😆. 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) } //complete the function function avg(){ } 1. Didn’t use “function avg()” part,output is still right, what is that for? 2. Where should I use “return statement ”?
13 Answers
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
Thank you for the detiled answer😍, now I know that the purpose of using a function is so that it can be used repeatedly.
But I still don’t understand why two functions are needed in this problem. What is the relationship between them? Why not just call out the pervious function and print it out🤔?
+ 4
Liangyong Peng
functions are used for reusable code means your same function can be use in many places. This is why because we have to avoid lengthy code. If same logic is working on many places then why we need to write same code again and again. Instead of doing that we can make a function and call that function at many places.
Your code is working because you have directly written your logic instead of in a function.
In this case if you a make a function and don't use that then that function will be useless.
+ 3
Martin Taylor
I too gaining much knowledge from your answers. I try to give best answer but your answers are very knowledgeable. You think in different ways maybe you do more research on programming. Though I work in Java but still don't know much but your answers gives me more knowledge.
+ 2
Liangyong Peng
Instead of returning value to a function you can directly print there. Here is another example how to print results without returning anything.
function avg() {
var aveg = (a + b + c) / 3;
console.log(aveg);
}
just call this function in main function
function main() {
avg();
}
This will also work.
+ 2
Martin Taylor
Yes you are right but I had given instant solution so I didn't think about your solution.😃😃😃
+ 1
Liangyong Peng
If you make a function and call that function to print returned value then you have to return in that function.
For example you have created a function avg so you have to return there and store returned value to a variable then print that
function avg() {
var num1 = parseInt(readLine(), 10);
var num2 = parseInt(readLine(), 10);
var num3 = parseInt(readLine(), 10);
return (num1 + num2 + num3) / 3;
}
Now call this function in main function and store returned value to a variable or you can directly print function.
function main() {
var aveg = avg();
console.log(aveg);
}
Or
function main() {
console.log(avg());
}
+ 1
Liangyong Peng
Since JavaScript course projects are running on Node JS Server so no need to call main function.
They said to make a function because they want you should know what is the purpose of function, how to use and why to use.
This is a common question of new learner because they see this small code so they think why do we need function when we can directly do that things but when you are going to work on large scale of projects then you have to make a reusable code which may be achieve through function.
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Got it, thx!
+ 1
Martin Taylor Thank you for the complete answer😍, I am now clearer about the concept, I’ll try your solution once I get home.
0
still you can do it suing Rest Parameter ==>ES6
function main(){
let res=avg(10,20,30);
console.log(res);
}
function avg(...nums){
return nums.reduce((a,b)=>a+b)/nums.length;
}
0
var num1 = parseInt(readLine(),10);
var num2 = parseInt(readLine(),10);
var num3 = parseInt(readLine(),10);
function avg(zalupa1, zalupa2, zalupa3){
return (zalupa1 + zalupa2 + zalupa3) / 3
}
console.log(avg(num1, num2, num3))