From 5070b0b3a0f249cab4cb8cca083c0258f6f0dfcf Mon Sep 17 00:00:00 2001 From: Aree-Vanier Date: Tue, 13 Mar 2018 19:45:20 -0400 Subject: [PATCH] Results now ordered based on vote count --- votes.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/votes.js b/votes.js index 1f25555..c091f34 100644 --- a/votes.js +++ b/votes.js @@ -60,10 +60,40 @@ Vote.prototype = { console.log(scores); + var order = []; + + for(var i = 0; i < this.options.length; i++){ + if(order.length == 0){ + order.push(i) + continue; + } + + count = parseInt(scores[i]); + + var inserted = false; + for(var a = 0; a < order.length; a++){ + if(count > parseInt(scores[order[a]])){ + order.splice(a,0,i); + inserted = true; + break; + } + else if(count == parseInt(scores[order[a]])){ + order.splice(a,0,i); + inserted = true; + break; + } + } + if(!inserted){ + order.splice(this.scores.length,0,i); + } + } + + console.log(order); + //Create output string var results = "\n"; - for(var i = 0; i < this.options.length; i++){ - results+=this.options[i]+"("+i+"): "+scores[i]+"\n"; + for(var i = 0; i < order.length; i++){ + results+=this.options[order[i]]+"("+order[i]+"): "+scores[order[i]]+"\n"; } //Send vote completion message