Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda