1

For my quiz, I want to get the highest number of all answers. So far it works, but when there are 4 answers and 2 said A is right and two said B is right, then the overall group-result should be WRONG. So I want to find out, if there are more than only one highest elements:

example: Math.max(1,2,3,4) ---> 4 Math.max(2,2,0,0) ---> 2 (I need to know that there are more than one highest elements)

Thanks, Marc

Marc
  • 137
  • 1
  • 12

3 Answers3

2

So write your own function for this, like this:

function maxFindAndCount(listOfNumbers) {
  var result = { max: null, count: 0 };
  result.max = Math.max(listOfNumbers);
  for (var l in listOfNumbers) {
    if (listOfNumbers[l] == result.max)
      result.count++;
  }
  return result;
}

Usage:

var listOfNumbers = [1,2,3,4,4];
var result = maxFindAndCount(listOfNumbers);
var max = result.max;
var count = result.count;
Liglo App
  • 3,719
  • 4
  • 30
  • 54
  • Caveat: `listOfNumbers` got to be an `object` for this. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in. Don't say `arrays` are also `objects` in Javascript, I mean `object`. – loveNoHate Aug 24 '14 at 13:28
  • You are wrong. It works like this (with nuances, but I guess he will not extend the array prototype and it is just much faster to type). However, if you want you can also use a classical `for` loop `for (var l = 0; l < listOfNumbers.length; l++) {...` – Liglo App Aug 24 '14 at 13:30
  • Array IS an enumerable type. It will just iterate over all indexes and eventually added properties. If he adds no properties to this array, it will work correctly. – Liglo App Aug 24 '14 at 13:34
  • Ok. Learned for today. Sunday. Holy day. – loveNoHate Aug 24 '14 at 14:02
  • hi, very good idea, but Math.max([1,2,3,4,4]) will result in NaN, because it must not be an array. How do I solve that? – Marc Aug 24 '14 at 14:20
  • You gotta use the solution the others sported with `.apply()`. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max#Example:_Using_Math.max. – loveNoHate Aug 24 '14 at 15:38
2

For this answer I will assume you have the answers put in an Array.

You can first use this function to find the max value of the array:

var maxValue = Math.max.apply(null, array);

Then you can find the amount of times this value occurs by using the following:

array.sort();
var nrOfMaxValue = array.lastIndexOf(maxValue) - array.indexOf(maxValue);

Hope this works for you!

edit: I just figured, if you only want to know if the max answer was given more than once you can just do this, without sorting the array:

var maxValue = Math.max.apply(null, array);
var onlyOnceAnswered = array.lastIndexOf(maxValue) == array.indexOf(maxValue);
  • It's very elegant, but I wonder if it is faster than my code. For small arrays of course fully irrelevant. Much less to type. +1 – Liglo App Aug 24 '14 at 13:36
  • this worked out best for me. I really like the onlyOnceAnswered code, its simple, but functional. thanks – Marc Aug 24 '14 at 14:38
0
function highscores(arr){
    var max= Math.max.apply(Array, arr);
    return arr.filter(function(itm){
        return itm=== max;
    });
}

var A= [2, 3, 4, 4];

highscores(A)

returned value: (Array)[4, 4]
kennebec
  • 102,654
  • 32
  • 106
  • 127