Weird names but yours look better - will study it. Thanks!
Mine works but …guess it’s really junior-ish:
/*Given an arry of test results (each with a Student ID, and the student’s Score), return the Final Score for each student. A student’s Final Score is calculated as the average of his/her 5 highest test scores. You can assume each student has at least 5 test scores.
*/
const scores = [
{id: 1, score: 50},
{id: 2, score: 80},
{id: 1, score: 100},
{id: 2, score: 80},
{id: 1, score: 100},
{id: 2, score: 60},
{id: 3, score: 70},
{id: 2, score: 80},
{id: 1, score: 40},
{id: 2, score: 80},
{id: 3, score: 90},
{id: 3, score: 90},
{id: 1, score: 95},
{id: 3, score: 90},
{id: 1, score: 100},
{id: 3, score: 90},
{id: 1, score: 100},
{id: 1, score: 100}];
scores.sort(function (x, y) {
return x.id - y.id || y.score - x.score;
});
function getFinalScores (scores) {
let currentID = 1;
let scoreCount = 0;
let totalScore = 0;
let finalScore = 0;
let avgScore= 0;
let topNumOfScores = 5;
scores.forEach(function(currentRow)
{
if (currentID == currentRow.id)
{
scoreCount +=1;
if (scoreCount <= 5) {
totalScore += currentRow.score;
}
}
if (currentID != currentRow.id)
{
console.log ("For ID " + currentID)
console.log ("Score Count is " + scoreCount)
console.log ("Total Score is " + totalScore)
console.log ("Average Score is " + totalScore/topNumOfScores);
currentID = currentRow.id;
scoreCount = 0;
totalScore = 0;
scoreCount +=1;
totalScore += currentRow.score;
}
}) //end forEach
console.log ("For ID " + currentID)
console.log ("Score Count is " + scoreCount)
console.log ("Total Score is " + totalScore)
console.log ("Average Score is " + totalScore/scoreCount)
}
getFinalScores(scores);