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 9173489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:35:48+00:00 2026-06-17T16:35:48+00:00

I’m still working on a program and I have a problem. The code is

  • 0

I’m still working on a program and I have a problem. The code is here:

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

    for(j=0; j<=10; j++) {
            var r = Math.floor(Math.random() * 20);

            if (elev[r].Used) {             //1st number
                for(var a=0; a<=21; a++) {
                    if (!elev[a].Used) {
                        elev[r].Value = elev[a].Value;
                        break;
                        }
                    else {continue; }
                    }
                }
                ...
                }
            }


            elev[r].used = true;
            doument.write(elev[r].Value);
            ...
        }
}

First of all, I have 22 imports to the var inputs[i].value (later elev[i]) in my HTML document. I want it to use all of the ‘elev’ arrays once, but I see (in my document.write as I clipped out here) it uses some of them twice and some of them isn’t even used. How can I fix that?

Problem solved by using 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-17T16:35:49+00:00Added an answer on June 17, 2026 at 4:35 pm

    Let me point out several problems with your script

    First of all this section

    for(i=0; i<inputs.length; i++) {
       elev[i] = inputs[i].value;
       elev[i].used = false;
    }
    

    You are assigning the value to the array but it is of type String. String does not get the properties.

    The following code will work

    var obj = {};
    obj.SomeProp = true;
    

    But if you do so

    var str = "";
    str.SomeProp = true;
    

    attempt to use str.SomeProp will always return undefined
    The code if (elev[r].used) will always be evaluated to false (because 0, empty string, false, null and undefined behaves as false in if operator in JavaScript)
    So I suggest you to refactor your code into

    for(i=0; i<inputs.length; i++) {
        elev[i] = {
            "Value": inputs[i].value,
            "Used": false
        }; // Pay attention, I added an object and Used property starts with an uppercase letter.
        // Then further in the code you'll use elev[index].Value in order to use the value
    }
    

    And finally the block if (elev[aa].used = false) will not throw exception, but this is a error.

    Consider the following snipped

    var someVal = true;
    if (someVal = false)
        alert("That's wierd");
    alert(someVal);
    

    It will work because you are assigning the value false to the someVal variable and this instruction is evaluated to true. Some the alert with message That's wierd will appear. Furthermore, you’ll notice that the someVal‘s value is false.
    This snipped uses the equality operator which will be evaluated to false and the first alert will not work out. However there is a problem too. A stylistic one. Never check the value for false, especially in JavaScript.

    var someVal = true;
    if (someVal == false)
        alert("That's wierd");
    alert(someVal);
    

    The correct snippet must look like this.

    var someVal = true;
    if (someVal)
        alert("There you go!");
    alert(someVal);
    

    P.S.

    You get the variable aa from nowhere. Where does it come from? It must be undefined. What did you want?
    Concatinated values of two numbers? The sum of them?

    EDIT:

    Just a few notices.

    1) Use for (var a = 0; a < someConstant; a++) syntax. Don’t forget to add var keyword.

    2) Use proper indention, i.e.

    if (someCondition){
        // Some instruction
        for (var i = 0; i < 10; i++) {
            //more indention
        }
    }
    

    3) Don’t use empty else block.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.