Why is this printing NaN? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is this printing NaN?

EDIT: onst grades = [ { value: 'null', label: 'None', }, { value: 10, label: 'O', }, { value: 9, label: 'A+', }, { value: 8, label: 'A', } ]; export default function Calculator() { const n = 5; var [matrix, setMatrix] = React.useState(Array.from({length: n},()=> Array.from({length: 3}, () => null))); const handleChange = (row, column, event) => { let copy = [...matrix]; copy[row][column] = event.target.value; setMatrix(copy); console.log(matrix); }; const gpaCalculate = () => { let copy = [...matrix]; var credit, grade, sumpoints, sumCredits; copy.map(rows => { if(rows[1]=="") { credit = 0; } else { credit = parseInt(rows[1]); } grade = parseInt(rows[2]); console.log(credit, grade); console.log(typeof(credit), typeof(grade)); sumpoints += parseInt(credit * grade); sumCredits += parseInt(credit); console.log(sumpoints, sumCredits); }) console.log(sumpoints, sumCredits); var gpa = (sumpoints/sumCredits); console.log("GPA", gpa); }; return ( <Box sx={{ px: 20, m: 3 }}> <Grid container justifyContent="center" alignItems="center"> <Grid item xs={12} sm={8}> <Typography variant="h1">GPA Calculator</Typography> <TableContainer component={ Paper } elevation={ 3 } sx={{ width: 'max-content' }}> <Table aria-label="GPA calculating table"> <TableHead> <TableRow> <TableCell>Sr.</TableCell> <TableCell>Course</TableCell> <TableCell

25th Nov 2021, 12:49 PM
Adarsh Mamgain
Adarsh Mamgain - avatar
2 Answers
+ 3
This code cannot output anything to the console because there is no call to graCalculate() and no matrix value. 🤔
25th Nov 2021, 1:09 PM
Solo
Solo - avatar
+ 3
As Vasilly said, the function is not being called, and missing info in what <matrix> is. Please rather save a copy of the code in SoloLearn and share its link. With more info revealed, hopefully we can work something out. https://www.sololearn.com/post/75089/?ref=app
25th Nov 2021, 1:13 PM
Ipang