+ 1
27.2 Average of Three Javascript
Can anyone tell me what's wrong with this code: 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) / 3; return average; }
3 Réponses
+ 5
Calling the function avg with 3 numbers passing will give back or return a value with calculating average, which should be assigned to average variable.
You don't have any value assigned to variable. And function is unused still. So do above steps..
If you Don't know: complete functions lesson..
Hope it helps..
+ 2
function main()
{
var num1 = parseInt(readLine(),10);
var num2 = parseInt(readLine(),10);
var num3 = parseInt(readLine(),10);
var average = avg(num1 ,num2 ,num3 );
//assigns the average value to the variable average, which is calculated and returned from function avg.
console.log(average);
}
//complete the function
function avg(num1,num2,num3){
var average = (num1 + num2 +num3) / 3;
return average;
}
//this is code what is asked to write. Use of function and getting a return value
+ 1
I improvised and got the right result, but I would still like to know what the clean code for this challenge looks like.
//complete the function
function avg(){
var num1 = parseInt(readLine(),10);
var num2 = parseInt(readLine(),10);
var num3 = parseInt(readLine(),10);
console.log ((num1 + num2 +num3) / 3);
}
avg();