Rank numeric values and show the top 3

Sep 08, 2023

I like to rank numeric values with a Javascript. I have a result page with 4 different variables. I like to rank the values and show the top 3 values. I used Javascript, but it doesn´t work. Can anybody help me? I attached my code. The problem is that the 3 textboxes I created do not show the ranked values. I appreciate any help ;-).

// Get the variables value out of Storyline project
var varValueName1 = player.GetVar("Name1");
var varValueName2 = player.GetVar("Name2");
var varValueName3 = player.GetVar("Name3");
var varValueName4 = player.GetVar("Name4");

// Create an array of your variables
var varArray = [varValueName1, varValueNam2, varValueName3, varValueName4];

// Use the sort() method with a custom comparison function varArray.sort(function(a, b) { return b - a; // Sort in descending order });

// Update the content of textboxes with the sorted values document.getElementById("Text-feld1").textContent = varArray[0]; document.getElementById("Text-feld2").textContent = varArray[1]; document.getElementById("Text-feld3").textContent = varArray[2];

3 Replies