Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!