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

  • Home
  • SEARCH
  • 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 9194327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:23:07+00:00 2026-06-17T21:23:07+00:00

So I’m working on this script. When I’m done with it, it should be

  • 0

So I’m working on this script. When I’m done with it, it should be used for making 2-and-2 groups. But anyway; The ‘input’ array in the start of the script, will get 22 different inputs from my HTML file. As a standard, I gave them the values 1-22. The thing is my two blocks ‘1st number’ and ‘2nd number’ doesn’t work very well: they don’t return the right numbers. Cause I want every elev[x] to be used once! Not 2 times, not 0 times, once! And the blocks returns like some of them twice and some of them isn’t even used. So how can I fix this?

    function Calculate(){
    var elev = [];
    var inputs = document.getElementsByName("txt");
    for(i=0; i<inputs.length; i++) {
    elev[i] = {
        "Value": inputs[i].value,
        "Used": false
    };
}

    function shuffle(elev){
    var len = elev.length;
    for(i=1; i<len; i++){
        j = ~~(Math.random()*(i+1)); 
        var temp = elev[i];
        arr[i] = elev[j];
        arr[j] = temp;
        }
    }

    for(var v=0; v<1; v++) {
        shuffle(elev);
        document.write(elev + '<br/>\n');
    }}

Yes, I’m still new at programming and I just wanna learn what I can learn.

Problem solved by doing the Fisher-Yates shuffle.

  • 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-06-17T21:23:09+00:00Added an answer on June 17, 2026 at 9:23 pm

    The idea of shuffling an array and iterating over it is correct, however, sorting by a coin-flipping comparator (a common mis-implementation; suggested by the other answer) is not the correct way to shuffle an array; it produces very skewed results and is not even guaranteed to finish.

    Unless you’re willing to fetch underscore.js (arr = _.shuffle(arr)), It is recommended to use the Fisher-Yates shuffle:

    function shuffle(arr){
      var len = arr.length;
      for(var i=1; i<len; i++){
        var j = ~~(Math.random()*(i+1)); // ~~ = cast to integer
        var temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
    
    ...
    
    shuffle(elev);
    for(var i=0; i<elev.length; i++){
      //do something with elev[i]
      console.log(elev[i].Value);
    }
    

    also, note that object fields should be lowercase, not uppercase (value', not 'Value'). Also, theUsed` field should not be neccessary since you now iterate in order. Also, any chance you could use an array of Values? They seem to be the only field in your objects.

    Also, Don’t use document.write(). It doesn’t work as expected when used after the page load. If you want to append something to the document, you hate jQuery and other frameworks and you don’t want to go the long way of creating and composing DOM nodes, document.body.innerHTML += is still better than document.write() (but still consider appending DocumentFragments instead’).

    demo: http://jsfiddle.net/honnza/2GSDd/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am confused How to use looping for Json response Array in another Array.

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.