+ 2

Not Sure Why I'm getting NaN on this code

Trying to solve this ES6 Class Methods Practice Problem and this is my code so far: You are making a program so that students are able to calculate their average of 3 exam scores. The given program takes the scores of 3 exams as input and declares the Exams class. Add a static method average() to class Exams, which will take the scores as parameters, and calculate and return the average score rounded to the nearest integer so that the code in main works correctly. function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1,exam2,exam3)); } class Exams{ //your code goes here constructor(x,y,z) { this.x = x; this.y = y; this.z = z; } static average(a) { const avg = ((a.x + a.y + a.z)/3); return Math.round(avg); }; }; I'm just a little confused as to why the function is returning NaN when I run it.

5th Apr 2021, 7:07 AM
Arthur Laffer
12 Respuestas
+ 2
1. You don't need the constructor function. 2. Your average function only has one parameter - a - but Sololearn will call it with 3 arguments because there are 3 scores. 3. In your code, a.x, a.y, a.z will not work because that's like saying 20.x, 50.y, 70.z or something like that.
5th Apr 2021, 8:11 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 14
function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1,exam2,exam3)); } class Exams{ //your code goes here static average (a, b, c) { const avg = (a + b + c)/3; return Math.round(avg); }; }; Good Luck
25th Jan 2022, 8:53 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
It's a pro challenge, so i don't know if the constructor is already there. If it is, it is a red herring (you don't need it). The average method is static, so it is bound to the class and not an object of the class. Also because it is static you call it as Exams.average. It is supposed to take the 3 scores as parameters. That's why it is called like Exams.average(exam1, exam2, exam3) from main. Those are just 3 integers, no Exams object is created. The average method has to match that signature, so it has to take 3 parameters (integers) and return the average of those
29th Apr 2021, 9:03 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Melanie Müller This is what my code looked like after I removed the constructor and fixed the average code accordingly function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1,exam2,exam3)); } class Exams{ //your code goes here static average (a, b, c) { const avg = (a + b + c)/3; return Math.round(avg); }; };
29th Apr 2021, 9:25 PM
Arthur Laffer
+ 1
function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1,exam2,exam3)); } class Exams{ //your code goes here static average(exam1,exam2,exam3){ return Math.round((exam1+exam2+exam3)/3); } }
6th Nov 2022, 10:05 PM
Khouloud Mekni
Khouloud Mekni - avatar
0
But what's the solution? I am at the same point. function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1,exam2,exam3)); } class Exams{ //your code goes here constructor(exam1,exam2,exam3){ this.exam1 = exam1 this.exam2 = exam2 this.exam3 = exam3 } static average (a) { return Math.round((a.exam1 + a.exam2 + a.exam3)/3); } }
29th Apr 2021, 8:26 PM
Melanie Müller
Melanie Müller - avatar
0
Thank you guys. Know I understand my failure.
30th Apr 2021, 4:32 AM
Melanie Müller
Melanie Müller - avatar
0
function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1,exam2,exam3)); } class Exams{ //your code goes here static average (a, b, c) { const avg = (a + b + c)/3; return Math.round(avg); }; };
14th Nov 2022, 10:48 AM
Daniela Bulimar
Daniela Bulimar - avatar
0
Wher the given inpout for the exam goes?
9th Apr 2023, 3:15 AM
Adolphe Tshitende
Adolphe Tshitende - avatar
0
function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1, exam2, exam3)); } class Exams { static average(exam1, exam2, exam3) { // Calculate the average and round it to the nearest integer return Math.round((exam1 + exam2 + exam3) / 3); } }
6th Oct 2023, 1:33 PM
Sina Ghorbanian
Sina Ghorbanian - avatar
0
when the object is passed as a parameter you can call it as a.x, a.y, a.z but here we just passing the variable so you can add it directly. and why did you pass "a" as an parameter in our main method we are not creating any object and passing it to the average() static function
15th Apr 2024, 8:25 AM
Priya Achuthan
Priya Achuthan - avatar
- 1
Your average function expects an Exams object but you are feeding it 3 integers
5th Apr 2021, 8:02 AM
Benjamin Jürgens
Benjamin Jürgens - avatar