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

186
Visualizações
Shuffle array of N letters and take M of them

I have an X array of objects "letter":"frequency" and i'm trying to build a new Y array from the previous one made of frequency-times letters (for each present in X ary). Then my purpose is to shuffle Y ary and take just the z-first elements from that. n is an argument passed to the function

exports.listLetters = (n) => {
return new Promise((resolve, reject) => {
    const sql = 'SELECT * FROM letters';
    db.all(sql, [], (err, rows) => {
        if (err) {
            reject(err);
            return;
        }
        if (rows == undefined) {
            resolve({ error: "Errors with retrieving letters. " })
        } else {

            const letters = rows.map((e) => ({ letter: e.letter, frequency: e.frequency }));const letters = rows.map((e) => ({ letter: e.letter, frequency: e.frequency }));
            let freqLetters = [];
            for (let letter in letters) {
                for (let i = 0; i <= letter.frequency * 100; i++) {
                    freqLetters.push(letter);
                }
            };

            shuffleArray(freqLetters);
            freqLetters.slice(0, n);

            resolve(freqLetters);

Why this algorithm doesn't work? Shuffle function is:

function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The big problem that make your code doesn't work is you can't directly use for in to loop through the array contains array. You doesn't define the rows and letter so I create one for showing purpose.

You missed the property name, I would suugest you to use forEach as an alternative option.

Also, slice doesn't change on the original array, it creates a copy, so you should define another array.

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end

let rows = [{
    letter: 'word',
    frequency: 5
  },
  {
    letter: 'yes',
    frequency: 4
  },
  {
    letter: 'no',
    frequency: 3
  }
]
let n = 10
const letters = rows.map((e) => ({
  letter: e.letter,
  frequency: e.frequency
}));

let freqLetters = [];

letters.forEach(function(arr) {
  for (let i = 0; i <= arr.frequency * 100; i++)
    freqLetters.push(arr.letter);


})

shuffleArray(freqLetters);
let finalarray = freqLetters.slice(0, n)
console.log(finalarray)
function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
}

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