Forum Discussion

MichellePike-56's avatar
MichellePike-56
Community Member
3 days ago

Sort numbered variables

I have 8 number variables, all that are added to depending on what the learner picks throughout a quiz. These variables will then suggest the top three courses to do. Is there a way to sort the number variables or an easy way to get the top three out of the 8? 

  • JesseWu's avatar
    JesseWu
    Community Member

    // Get the values of the 8 variables and put them in an array

    var variables = [ player.GetVar("Variable1"),

    player.GetVar("Variable2"),

    player.GetVar("Variable3"),

    player.GetVar("Variable4"),

    player.GetVar("Variable5"),

    player.GetVar("Variable6"),

    player.GetVar("Variable7"),

    player.GetVar("Variable8") ];

    // Sort the array in descending order and get the top three values

    var topThree = variables.sort((a, b) => b - a).slice(0, 3);

    // Set the top three values back to Articulate variables if needed

    player.SetVar("Top1", topThree[0]);

    player.SetVar("Top2", topThree[1]);

    player.SetVar("Top3", topThree[2]);

    JS Script could be your helping hand.

    • MichellePike-56's avatar
      MichellePike-56
      Community Member

      Thank you, I was thinking JS may be the answer but I don't know much about it so I will give this a go and see what happens. Thank you 

    • MichellePike-56's avatar
      MichellePike-56
      Community Member

      Is the "Top1", "Top2", "Top3" the variables to report back into articulate to use to show the answers?

      • JesseWu's avatar
        JesseWu
        Community Member

        Yes. You create those variables in Articulate to host the result before you run the script. This script will assign them values when it is done running.