sarru
March 3, 2026, 9:50pm
1
Tell us what’s happening:
my last function not working properly, I have put first two functions in variables and trying to use it in last function but no luck.
Your code so far
function getAverage(scores)
{
let sum=0;
let average;
for(let char of scores)
{
sum += char;
average=sum/scores.length;
}
return average;
}
let averageGrade= getAverage([2,2]);
console.log(averageGrade);
function getGrade(grade)
{
let result;
if(grade==100 )
{
result="A+";
}
else if(grade>=90 && grade <=99)
{
result="A";
}
else if(grade>=80 && grade <=89)
{
result="B";
}
else if(grade>=70 && grade <=79)
{
result="C";
}
else if(grade>=60 && grade <=69)
{
result="D";
}
else if(grade>=0 && grade <=59)
{
result="F";
}
return result;
}
let studentGrade=getGrade(40);
console.log(studentGrade);
function hasPassingGrade(num)
{
let result1;
if(num>=59)
{
result1=true;
}
else
{
result1=false;
}
return result1
}
let passingResult=hasPassingGrade(40);
console.log(passingResult);
function studentMsg(averageGrade,studentGrade)
{
let massage;
if(studentGrade=="F")
{
massage=`Class average: ${averageGrade}. Your grade: ${studentGrade}. You failed the course.`
}
else
{
massage=`Class average: ${averageGrade}. Your grade: ${studentGrade}. You passed the course.`
}
return massage;
}
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Challenge Information:
Build a Gradebook App - Build a Gradebook App
ILM
March 3, 2026, 9:54pm
2
sarru:
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));
if the function is called like this
that means that averageGrade is going to be an array, and studentGrade is going to be 37, right?
sarru:
if(studentGrade=="F")
so here you get a number compared to "F"
and here
you are putting that array inside the string
sarru
March 3, 2026, 10:00pm
3
what im expecting is since I have put ${averageGrade}. and ${studentGrade} two variable which stored the functions so it shud calculate the average and return alphabet for garde by considering all what I have done in function above so I do not need to repeat the function. If im wrong pls clarify.
ILM
March 3, 2026, 10:02pm
4
this variable here exists outside the function
inside the function averageGrade is the value passed to the function parameter
see averageGrade here in the function definition?
that means that the value is the one passed in when the function is called
sarru:
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));
in this case [92, 88, 12, 77, 57, 100, 67, 38, 97, 89]
sarru
March 3, 2026, 10:05pm
5
Ok, I got it but what is the other way of using those two functions calculations here, then
sarru
March 3, 2026, 10:05pm
6
I thought that is global var so can be used in other function
ILM
March 3, 2026, 10:10pm
7
you need to use the functions inside the function, if you use them outside the function you can’t get the values of the function parameters
sarru
March 3, 2026, 10:16pm
8
Tried this and I can see average but the comparison with “F” is not fine it seems as all showing failed with “F” garde.
function studentMsg(scoreArr,stdGrade)
{
let array=getAverage(scoreArr);
let marks=getGrade(stdGrade);
let massage;
if(studentGrade=="F")
{
massage=`Class average: ${array}. Your grade: ${studentGrade}. You failed the course.`
}
else
{
massage=`Class average: ${marks}. Your grade: ${studentGrade}. You passed the course.`
}
return massage;
}
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));
console.log(studentMsg([56, 23, 89, 42, 75, 11, 68, 34, 91, 19], 100));
console.log(studentMsg([12, 22, 32, 42, 52, 62, 72, 92], 85));
ILM
March 3, 2026, 10:17pm
9
if you have this, where does studentGrade variable take its value?
also, why are these different?
sarru
March 3, 2026, 10:20pm
10
putting marks
if(marks=="F")
showing this now:
Class average: 71.7. Your grade: F. You failed the course.
Class average: A+. Your grade: F. You passed the course.
Class average: B. Your grade: F. You passed the course.
ILM
March 3, 2026, 10:22pm
11
sarru:
Class average: A+.
should this be the average as a score or as a numerical value?
sarru:
Your grade: F.
this is always F, where does studentGrade come from?
sarru
March 3, 2026, 10:23pm
12
go thte silly mistake and its passed now, it was diff values here in message and comparison with marks worked, thanks for your patience