Return the Top 3 scorers from an Array

Sep 09, 2022

Problem Statement  - user takes a multi-category test(Science, Math, English, History, Social Science). Basis the score, it shall return which is his/her strongest subject and which one is weakest.

I tried using this JS. 

let player = GetPlayer();

let aScore = player.GetVar("a");// Math Score
let bScore = player.GetVar("b");//Science Score
let cScore = player.GetVar("c");//History Score
let dScore = player.GetVar("d");//Social Science Score
let eScore = player.GetVar("e");//English Score


let semester = [
    { Cat:'Math', Score: aScore},
    { Cat:'Science', Score: bScore},
    {Cat:'History', Score: cScore},
    {Cat:'Social Science', Score: dScore},
    { Cat:'English', Score: eScore},
  
];


let x = semester.sort(function (x, y) {
    return x.Score - y.Score;
});

    
player.SetVar("x",x);// I tried using the text and numeric field
for x in storyline field. 

After publishing when I submit calculate this is the error thrown"

[object Object],[object Object],[object Object],[object Object],[object Object]

Can any one help further and suggest a turnaround or identify the error?

1 Reply