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

149
Vistas
Js secret santa claus algoritm

I wanted to make a small script in js that having a list of users, one user has to make a gift to another.

By applying the following constraints:

  1. If "a" is the santa claus and gives a gift to "c" it cannot be the other way around. So "c" cannot be the santa claus of "a".

  2. It must work with both an even and an odd number of users.

In your opinion, what could be the right approach to use to try to minimize the number of comparisons, that is, speed up the script.

I was thinking something like this to start, but afterwards I'm not sure how to proceed:

let name = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

let a = [...name];
let group1 = [];
let groupSanta = [];
let groupUser = [];

for (var i = 0; i < name.length / 2 - 1; i++) {
  let santaClaus = a[Math.floor(Math.random() * a.length)];
  a = a.filter(item => item !== santaClaus);
  let user = a[Math.floor(Math.random() * a.length)];
  a = a.filter(item => item !== user);
  group1.push({ santaClaus, user });
}

console.log(a, group1);

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can just randomly sort the array and assign each person to the next one. Then assign the first person to the last one in the array

// Define names
const names = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

// Function to shuffle array
const shuffle = (arr) => {
    for (let i = arr.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }
    return arr;
}

const randomNames = shuffle(names);

// Match each person with the next one, folding over at the end
const matches = randomNames.map((name, index) => {
  return {
    santa: name,
    receiver: randomNames[index + 1] || randomNames[0],
  }
});

console.log(matches);

about 4 years ago · Juan Pablo Isaza Denunciar

0

let players = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

players = shuffleArray(players)

const matches = players.map((name, index) => {
  return {
    santa: name,
    receiver: players[index + 1] || players[0],
  }
});

function shuffleArray(array) {
    let currentIndex = array.length, randomIndex

    while (currentIndex != 0) {
        randomIndex = Math.floor(Math.random() * currentIndex)
        currentIndex--
        [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]
    }

    return array
}

console.log(matches)

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