Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined property error in my code, problem with loop while

I’ve been trying to find a solution, but I don’t know what’s wrong here.
I want to make a while loop, that while two element in an array are the same, push an array until there’s not left similar arrays

function calcularModa(listaUser){
const lista = listaUser;
const listaCount = {};
lista.map(
    function (elemento){
        if (listaCount[elemento]){
            listaCount[elemento] += 1;
        } else{
            listaCount[elemento] = 1;
        }
    }
);
const listaArray = Object.entries(listaCount).sort(
    function  (valorAcumulado, nuevoValor) {
        return nuevoValor[1] - valorAcumulado[1];
    }
);

let moda;
if (listaArray[0][1] != listaArray[1][1]){
    moda = listaArray[0];
    return moda;
}
moda = [listaArray[0]]
let i = 1;
while(listaArray[0][1] == listaArray[i][1])
{
moda.push(listaArray[i])
i++;    
}
return moda;
}
  calcularModa([1,1,2,2,3,3]);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your final loop is accessing listaArray[i][1] without first checking that i is still less than the length of that array. So add i < listaArray.length as a condition in that while loop.

Not your question, but:

  • There is no reason to treat a separate case in the if that precedes that loop. Just remove that part: the while loop should take care of it.
  • Don’t use .map when you just want to iterate and not really map. A for..of loop would be appropriate here.
  • As your input seems to consist of numbers, you need to make the conversion from string back to number again. Object keys are never of the number type.

Your code could look like this (still with the while loop):

function calcularModa(listaUser) {
  const lista = listaUser;
  const listaCount = {};
  for (let elemento of lista) {
      if (listaCount[elemento]) {
        listaCount[elemento] += 1;
      } else {
        listaCount[elemento] = 1;
      }
  }
  const listaArray = Object.entries(listaCount).sort(
    function(valorAcumulado, nuevoValor) {
      return nuevoValor[1] - valorAcumulado[1];
    }
  );

  let i = 1;
  while (i < listaArray.length && listaArray[0][1] == listaArray[i][1]) {
    i++;
  }
  return listaArray.slice(0, i).map(([key]) => key).map(Number);
}

let moda = calcularModa([1,2,4,3,2,3,5]);
console.log(moda);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading