Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:05+00:00 2026-05-29T09:45:05+00:00

I tried to write some javascript that takes the entries of an array and

  • 0

I tried to write some javascript that takes the entries of an array and shuffles the order. It’s not compiling like it should though. Seems to only run through the for loop once. What am I missing?

//random number between 1 and num
function randInt(num){
    return Math.floor(num*Math.random()+1);
}

//shuffles deck (array) of any size
function shuffle(array){
    var newArray = new Array();
    var n = array.length;
    for(i=0; i<n; i++){
        var entry = randInt(array.length) - 1;
        newArray[i] = array[entry]; //assigns random entry in initial array to       new array
            array = array.splice(entry, 1); //removes the entry that was stored into newArray
    }
    array = newArray;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:45:06+00:00Added an answer on May 29, 2026 at 9:45 am
    • array.splice modifies array and returns the removed item(s). You want to discard the element, so just do this instead of overwriting array:
    array.splice(entry, 1);
    
    • + 1 in randInt and doing - 1 afterwards seems superflouous.
    • Use var i = 0 (though look at my last point).
    • Use [] instead of new Array() since the latter is not generally used.
    • Return the new array instead of overwriting array:
    return newArray;
    
    • You modify array so you cannot loop up to n anymore as the length becomes 1 less each time. You may want while(array.length > 0) { ... } instead of the for loop.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a small JavaScript (framework) that can chain functions with all
I am trying to write some front end Javascript tests that can test drag
I would like to have some Javascript code running in a web browser write
I tried to write a function to check key code with JavaScript. It's working
I tried to write a function that calculates a hamming distance between two codewords
i'm quite a beginner in C# , i tried to write a program that
I'm writing a utility that takes in a .resx file and creates a javascript
I'm trying to write some javascript functions to integrate with the Facebook stream. However
Using the HTML5 Geolocation API I've ended up with some variables in Javascript that
Some background: On a recent project, I tried to write a streamlined jQuery plugin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.