Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
How do i get the X number of unique items from an array

Im trying to change the X number in the for loop based on what array this function is using. The function gets x random values from an array which he then checks if it isnt the same value and if it isnt the same value he then returns the 2 random values in the array. i tried doing a switch statement like this:

switch(this) {
   case array1:
       x = 2;
       break;
   case array2:
       x = 3;
       break;
}

code

Array.prototype.random = function () {
    let result = [];
    let bool = false;
    let x = 0;
    for (var i = 0; i < x; i++) {
        result.push(this[Math.floor(Math.random() * this.length)]);
    }
    while (bool == false) {
        if (result[0] === result[1]) {
            result.pop();
            result.push(this[Math.floor(Math.random() * this.length)]);
        } else {
            bool = true;
        }
    }
    return result[0] + "  +  " + result[1];
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Getting the amount of unique items in the array looks something like

function getUniqueCount(list){
  let storage={}, count=0;
  for(let i=0; i<list.length; i++){
    if(!storage[list[i]]){
      storage[list[i]]=true; count++;
    }
  }
  return count;
}

But I'm not sure if that number would help you because you're not saying what you want your function to do

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm not sure what the x variable is supposed to be. If you just want to return 2 random elements from the array, there's no need for that variable or the result array. Just use two variables.

Array.prototype.random = function() {
  let item1 = this[Math.floor(Math.random() * this.length)];
  while (true) {
    let item2 = this[Math.floor(Math.random() * this.length)];
    if (item2 != item1) {
      return item1 + "  +  " + item2;
    }
  }
}

console.log([1, 2, 3, 4, 5, 6, 7, 8].random());

about 4 years ago · Juan Pablo Isaza Denunciar

0

Remove "()this" because this is a syntax error.

You're trying to do a math calculation:

Math.random() * this.length

Not a syntax error:

Math.random()this.length

And also, you're for loop is not doing anything, because i counts up to x only if i is lower than x. But i and x are both 0, so it will not do anything. If you're trying to make the for loop go up 1 time, just use "2", for 2 results instead of "0".

Next, result.pop() is just returning the value of "result" popped.

Remove that.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda