1 INPUT –Use a Set node to simulate your list of subjects
{
“subjects”: [
{“subject”: “Math”, “grade”: 1.5, “units”: 3},
{“subject”: “Science”, “grade”: 1.75, “units”: 4},
{“subject”: “History”, “grade”: 2.0, “units”: 3}
]
}
2 Function Node for Calculation(code node)
// Get input data
const subjects = $json[“subjects”];
let totalGradePoints = 0;
let totalUnits = 0;
subjects.forEach(subj => {
totalGradePoints += subj.grade * subj.units;
totalUnits += subj.units;
});
const gwa = totalGradePoints / totalUnits;
// Return result
return [
{
json: {
totalGradePoints,
totalUnits,
GWA: parseFloat(gwa.toFixed(2))
}
}
];
- Output
{
“GWA”: 1.83,
“Total Grade Points”: 24.5,
“Total Units”: 13
}
Here’s a n8n workflow JSON for reference:
{
“nodes”: [
{
“parameters”: {
“values”: {
“boolean”: ,
“number”: ,
“string”: [
{
“name”: “subjects”,
“value”: “[{“subject”:“Math”,“grade”:1.5,“units”:3},{“subject”:“Science”,“grade”:1.75,“units”:4},{“subject”:“History”,“grade”:2.0,“units”:3}]”
}
]
},
“options”: {}
},
“name”: “Set Subjects”,
“type”: “n8n-nodes-base.set”,
“typeVersion”: 2,
“position”: [250, 300]
},
{
“parameters”: {
“functionCode”: “const subjects = JSON.parse($json[“subjects”]);\nlet totalGradePoints = 0;\nlet totalUnits = 0;\nsubjects.forEach(subj => {\n totalGradePoints += subj.grade * subj.units;\n totalUnits += subj.units;\n});\nconst gwa = totalGradePoints / totalUnits;\nreturn [{ json: { GWA: parseFloat(gwa.toFixed(2)), totalGradePoints, totalUnits } }];”
},
“name”: “Compute GWA”,
“type”: “n8n-nodes-base.function”,
“typeVersion”: 2,
“position”: [500, 300]
}
],
“connections”: {
“Set Subjects”: { “main”: [[{ “node”: “Compute GWA”, “type”: “main”, “index”: 0 }]] }
}
}