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

226
Views
getting an error while running my shuffle builddeck (explanation below)

Shuffling your deck

  1. Use a function declaration to create a function called shuffle that takes deck as an argument.
  2. Inside this function create a variable called "shuffledDeck" that takes deck as its value.
  3. Using "let" declare three new variables: currentIndex, whos value should equal the length of the deck array, and two more:
    • temporaryValue and randomIndex,
    • each of which should currently have no value assigned.
  4. Create a while loop whos condition is that "currentIndex" does not equal 0, so that we stop looping once we've gone through all 52 cards.
  5. Inside the while loop, use the javascript Math.methods to generate a random integer between 0 and "currentIndex"
  6. Inside the while loop, decrement current index by 1. (should be after step 9)
  7. Inside the while loop, assign "temporaryValue" with "shuffledDeck" (which is an array) to the [currentIndex].
  8. Still inside, assign "shuffledDeck[currentIndex]" a value of shuffledDeck to the [randomIndex]
  9. Still inside, assign "shuffledDeck[randomIndex]" a value of "temporaryValue". (currentIndex //i--;)
  10. Review the code from steps 7,8, and 9, and leave a comment explaining what you believe those lines of code are doing as they swap assignments of values between them.
  11. Finally, close the while loop and return "shuffledDeck". You should now be able to run shuffle(buildDeck()) in node and see your shuffled deck of cards.

Here is my code

function shuffle (deck) {
    let shuffleDeck = deck;
    let currentIndex = deck.length;
    let temporaryValue, randomIndex;

    while (currentIndex != 0) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = shuffleDeck[currentIndex];
        shuffleDeck[randomIndex] = temporaryValue;
    }

    return shuffleDeck;
}

I'm super new to coding so I'm just confused

about 4 years ago · Juan Pablo Isaza
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!