Finding the mode without an array?

Finding the mode without an array?
0

#1

Finding the mean and median were pretty easy, I was happy! This one is seeming to be a bit more difficult: finding the mode. The answer cannot be [10], it must be 10.

function mode(numbers) {
    //declare variables
  //array to be returned as answer
  let modes = []
  //count how many numbers in array
  let count = []
  let i;
  let num;
  //keep track of most consistent number
  let max = 0;
  
  for (i = 0; i < numbers.length; i++) {
      num = numbers[i];
      count[num] = (count[num] || 0) + 1;
      if (count[num] > max) {
          max = count[num];
      }
  }
    for (i in count)
        if (count.hasOwnProperty(i)) {
            if (count[i] === max) {
                modes.push(Number(i));
            }
        }
    
    return modes;
  

#2

What do they expect you to return if there are two modes? For example, what if the list was 1, 3, 3, 3, 4, 4, 6, 6, 6, 9. The modes would be 3 and 6.


#3

I’m not sure. That’s why I thought an array would be perfect for this situation. They only link this: https://www.khanacademy.org/math/statistics-probability/summarizing-quantitative-data/mean-median-basics/a/mean-median-and-mode-review


#4

Do the test cases tell you what to return when they run?

If they are expecting a number then give them a number if the length of the modes array is equal to 1, otherwise pass an array. See if that works. Maybe none of the tests cases have more than one mode?


#5

Of course I broke something…

 function mode(numbers) {
    //declare variables
  //array to be returned as answer
  let modes = []
  //count how many numbers in array
  let count = []
  let i;
  let num;
  //keep track of most consistent number
  let max = 0;
  
  for (i = 0; i < numbers.length; i++) {
      num = numbers[i];
      count[num] = (count[num] || 0) + 1;
      if (count[num] > max) {
          max = count[num];
      }
  }
    for (i in count)
        if (count.hasOwnProperty(i)) {
            if (count[i] === max) 
                modes.push(Number(i));
            else if (count[i].length === 1){
                return count[i]
            }
                
            }
    }
    
    return modes

#6

Where is your { in this line?


#7

Thanks…I currently have this but it still gives me back an array if it is one number.

function mode(numbers) {
  let modes = []
  let count = []
  let i;
  let num;
  let max = 0;
  
  for (i = 0; i < numbers.length; i += 1) {
      num = numbers[i];
      count[num] = (count[num] || 0) + 1;
      if (count[num] > max) {
          max = count[num];
      }
  }
    for (i in count){
        if (count.hasOwnProperty(i)) {
            if (count[i] === max) {
                modes.push(Number(i));
                return modes;
            }
        }else{
            return count[i];
        }
  
}
}

#8

What part of the code above do you think returns a number if the modes array 's length is 1?


#9

It isn’t there yet. I’m not sure if I need the else if statement or not.


#10

Gonna just give this one up for the day and call it early. I guess I’ll come back tomorrow.