Hi,
I am trying to take a number entry from a text field to use in some round Robin code to output the data.
When I type a number for the variable num e.g. var num=12; the program works fine, but if I try to do it from text field it does not work, even though I am using ParseInt() to convert the input into a number, I have also tried using valueOf() as well. When I enter into the text field I enter only even numbers. The output for the program when it works I will put below the code.
Any help would be appreciated.

<div>
Enter Round Robin number: <input type="text" name="test" id="insert">
<input type="button" onclick="calculate(1);
    firsthalf(); secondhalf()" value="Enter">
<script>




    // change the value of num for round robbins, only an even value will work.
        var a = document.getElementById("insert").value;

        var num = parseInt(a);
    // var num = 12; instead  would run the program fine
    // creating my arrays, poslow is for the first half of the numbers, poshigh is for the second. 
    var poslow = [];
    var poshigh = [];
    var demi = num/2;

    // setting the array lengths based on half of what the numerical input is
    poslow.length = num/2;
    poshigh.length = num/2;
    // these two functions assign numerical values to each position in the array. so if the num value 
    // is 6 then first set creates the poslow array as poslow=[1,2,3]; so for secondset it woulc create poshigh = [4,5,6];

    function firstset(poslow)
    {
        for( j =0; j < poslow.length; j++)
        {

        poslow[j]=j+1;
        }
        return poslow;
    }

    function secondset(poshigh)
    {
        for(k = 0, l =num; k < poshigh.length, l > demi; k++, l--)
        {
            poshigh[k]=l;
        }
        return poshigh;
    }

    // the functions are added to variables poslow and poshigh so that they can be used in the rest of the programme

    poslow = firstset(poslow);
    poshigh = secondset(poshigh);

    // this function keeps the number 1 always in its original place, it then moves the first number in the array of poshigh
    // to the first number after the number one shifting all the other numbers up one place. The end of poslow is removed
    // and added to the end of poshigh.
    // this function also displays the numbers to the screen 
    function calculate(round, unshift, push)
    {

        document.write('<p style="color:red;">'+'round ' + round + ' </p>'+"<br/>");

        if ( typeof unshift != 'undefined' ) {

            poshigh.shift(); 

            poslow.pop();

            poslow.splice(0,1,1,unshift);

            poshigh.push(push);

        }

        for( j=0, k=0; j < poslow.length, k < poshigh.length; j++,k++)

        {       

            document.write('<p style="color:blue;">'+poslow[j]+" "+poshigh[k]+'</p>');

        }

    }

    // full and half are created for use in firsthalf and secondhalf functions
    // e.g. if the number was 12 the first half would be doing this: calculate(2, 12, 6); calculate(3, 11, 5); calculate(4, 10, 4); calculate(5, 9, 3);calculate(6, 8, 2);
    // the second half would be doing this: calculate(7, 7, 12);  calculate(8, 6, 11); calculate(9, 5, 10); calculate(10, 4, 9); calculate(11, 3, 8); calculate(12, 2, 7);


    var full = poslow.length+poshigh.length;
    var half = full/2; 

    // first half sets up the first half of the rounds 
    function firsthalf()
    {

        for (rnd =2, posone=full, postwo=half; rnd <= half, posone > half, postwo > 1; rnd++, posone--, postwo--)
        {
        calculate(rnd, posone, postwo);

        }
    }   

    function secondhalf()
    {
        for (rndb =half+1, posoneb=half+1, postwob=full; rndb <= full, posoneb > 2, postwob >=half+1; rndb++, posoneb--, postwob--) 
        {
            calculate(rndb,posoneb, postwob);

        }

    }


    // the functions are initialised, the first calculate covers the very first round, then firsthalf(); and secondhalf(); do the rest



  /*  document.write('<div style="float:left; padding-right:20px; color:red;">');

    document.write('</div>');
    document.write('<div style="float:left; color:red; padding-right:20px;">');

    document.write('</div>'); */ 







    </script>



</div>

round 1

1 12

2 11

3 10

4 9

5 8

6 7

round 2

1 11

12 10

2 9

3 8

4 7

5 6

round 3

1 10

11 9

12 8

2 7

3 6

4 5

round 4

1 9

10 8

11 7

12 6

2 5

3 4

round 5

1 8

9 7

10 6

11 5

12 4

2 3

round 6

1 7

8 6

9 5

10 4

11 3

12 2

round 7

1 6

7 5

8 4

9 3

10 2

11 12

round 8

1 5

6 4

7 3

8 2

9 12

10 11

round 9

1 4

5 3

6 2

7 12

8 11

9 10

round 10

1 3

4 2

5 12

6 11

7 10

8 9

round 11

1 2

3 12

4 11

5 10

6 9

7 8

round 12

1 12

2 11

3 10

4 9

5 8

6 7

Recommended Answers

All 2 Replies

You are executing lines such as var a = document.getElementById("insert").value; as soon as the document loads, instead of after the Enter button is clicked. The solution to this is to create another function, I've called it setup(), which you call when the Enter button is clicked and it sets up the variables you need in the whole script. I've cleaned up your code a bit, and taken out the comments (you can add them back in, it was just so I could see all the code in one place):

<div>Enter Round Robin number:
    <input type="text" name="test" id="insert" />
    <input type="button" onclick="setup(); calculate(1);
    firsthalf(); secondhalf()" value="Enter">
    <script>
        var num = "";
        var poslow = [];
        var poshigh = [];
        var demi = "";
        var full = "";
        var half = "";

        function firstset(poslow) {
            for (j = 0; j < poslow.length; j++) {
                poslow[j] = j + 1;
            }
            return poslow;
        }

        function secondset(poshigh) {
            for (k = 0, l = num; k < poshigh.length, l > demi; k++, l--) {
                poshigh[k] = l;
            }
            return poshigh;
        }

        function calculate(round, unshift, push) {
            document.write('<p style="color:red;">' + 'round ' + round + ' </p>' + "<br/>");
            if (typeof unshift != 'undefined') {
                poshigh.shift();
                poslow.pop();
                poslow.splice(0, 1, 1, unshift);
                poshigh.push(push);
            }
            for (j = 0, k = 0; j < poslow.length, k < poshigh.length; j++, k++) {
                document.write('<p style="color:blue;">' + poslow[j] + " " + poshigh[k] + '</p>');
            }
        }

        function firsthalf() {
            for (rnd = 2, posone = full, postwo = half; rnd <= half, posone > half, postwo > 1; rnd++, posone--, postwo--) {
                calculate(rnd, posone, postwo);
            }
        }

        function secondhalf() {
            for (rndb = half + 1, posoneb = half + 1, postwob = full; rndb <= full, posoneb > 2, postwob >= half + 1; rndb++, posoneb--, postwob--) {
                calculate(rndb, posoneb, postwob);
            }
        }

        function setup() {
            num = document.getElementById("insert").value;
            demi = num / 2;
            poslow.length = num / 2;
            poshigh.length = num / 2;
            poslow = firstset(poslow);
            poshigh = secondset(poshigh);
            full = poslow.length + poshigh.length;
            half = full / 2;
        }
    </script>
</div>

Hi Anthony,

Thank you. That makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.