Skip to content
Advertisement

Calculating the mode value of an array

The function for returning the mode value of an array works on every test except one. When I pass the following array through the function, I get a TypeError. I figured out that it has something to do with the number 0 in the passed array, but I don’t know why and I don’t know how to solve the problem. This is what my mode function and my typeerror function looks like.

 function mode (arr) {
  throwErrors(arr)

  const numberOccurence = {}
  const mostOccurent = []

  for (const numberValues of arr) {
    if (numberOccurence[numberValues]) {
      numberOccurence[numberValues] += 1
    } else {
      numberOccurence[numberValues] = 1
    } 
  }

  let timesOccured = Object.values(numberOccurence)
  let numbersSorted = timesOccured.sort((a, b) => b - a)

  for (const keys of Object.keys(numberOccurence)) {
    if (numberOccurence[keys] === numbersSorted[0]) {
      mostOccurent.push(Number(keys)) 
    }
  }

  return mostOccurent.sort((a, b) => a - b)
}


function throwErrors (functionParameter) {
  if (!Array.isArray(functionParameter)) { 
    throw new TypeError('The passed argument is not an array.')
  } else if (functionParameter.length === 0) {
    throw new Error('The passed array contains no elements.')
  } else if (!functionParameter.every(function checkIfNumber (elements) {
       if ((typeof elements == 'number')){
         return elements
       }
     })) { 
    throw new TypeError('The passed array may only contain valid numbers.')
  } 
}

If I pass [3, 5, 2, -5, 9, 2, -5, 5, 10, 4, 1, 0, -1, 9, 0] to the function, I get TypeError: “The passed array may only contain valid numbers”, but I expected to get [-5, 0, 2, 5, 9]

Advertisement

Answer

The callback function that you pass to Array.prototype.every() should return a boolean value. If it (the callback function) returns true for every element of array then only theevery method will return true.

Note this line in your callback :

if ((typeof elements == 'number')){
         return elements

You are returning element instead of boolean value. When the callback returns a 0 it is automatically casted to boolean false. and thus the every() returns false causing function to throw the TypeError.

Fix the callback :

//renaming `elements` to `element` as it's single element
//return the result of boolean expression instead of using if statement explicitly
function checkIfNumber (element) { 
       return typeof element == 'number';
}

Can be written as ES6 arrow function :

const checkIfNumber = element => typeof element == 'number';
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement