0

I'm very new to this, just trying to piece together snippets from other posts. I have a survey scale which you rate from 1 - 5. After that it will display your answer for each item. Survey results sample

I would like to get the majority of votes.

Sample in my screenshot is 5. Then I will display an equivalent result let's say 1 will display Factor A message and so on ... then 5 will display the message in Factor E. I am having a hard time doing it.

Can you give me an idea?

Here's the code for the output:

<div class="col-sm-3" id="scoreDiv">
<p>#1 - <span class="score" id="score1"></span></p>
<p>#2 - <span class="score" id="score2"> </span></p>
<p>#3 - <span class="score" id="score3"> </span></p>
<p>#4 - <span class="score" id="score4"> </span></p>
<p>#5 - <span class="score" id="score5"> </span></p></div>
Chenmunka
  • 685
  • 4
  • 21
  • 25
Raissa Ricarde
  • 108
  • 1
  • 11
  • 2
    What have you tried so far ? – andrew Sep 05 '18 at 00:12
  • I actually don't know where to start? First I tried to set a class for highest score but still no luck. This is actually different from what I wanted to do. http://jsfiddle.net/gsak6jfe/4/ – Raissa Ricarde Sep 05 '18 at 00:26
  • I am a beginner in jquery I can read and understand but I don't have an idea how to start to do it. – Raissa Ricarde Sep 05 '18 at 00:30
  • Well throw your result values in an array then do something like this to get the value with the highest frequency: https://stackoverflow.com/questions/3783950/get-the-item-that-appears-the-most-times-in-an-array – Iskandar Reza Sep 05 '18 at 00:33
  • but difference is it depends on what the user select in survey. I can't declare a definite number like var score = [1,2,2]. The value is display in – Raissa Ricarde Sep 05 '18 at 00:39

1 Answers1

0

try this:

 var _array = [];
 $("#scoreDiv p").each(function(index, el) {
 var text = $(this).text().split(" ");
    text = text[0].split("#");
   _array.push(text[1]);
 });
   alert(Math.max.apply(Math,_array));
Therichpost
  • 1,759
  • 2
  • 14
  • 19