How to loop an array in JavaScript









up vote
-1
down vote

favorite












I'm making a roulette using HTML/CSS/JS.
How it work: The roulette circles are a background-image that is made of 15 circles. 7red 7blue and 1green. The background-img is repeat-x so it never end. Then the javascript randomize a px position of the background img than the roulette image moves to that px. For an example if it randomize 17839, the roulette img will move to that point.
Now I'm trying to figure out how to tell the javascript on with color the roulette stoped.



My question is "How to loop an array(or copy) so it will have at least 2000 string Index of pattern below?"



I was trying to make a array:



var colors = ["red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "green"];


And then loop it somehow to make the array verry long. The max px that can be randomized is 20000. Every color circle is 100px. That make the whole patern 1500px long. That mean that I need a array that is at least 2000 string long. It feels wrong to write an array that long at my own so I'm trying to loop the color array to make it longer.



This is some example how I want it to work.



Random number is 1534.



random = 1534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 1, that mean that the color was red (array index 0)



Random number is 17534.



random = 17534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 11, that mean that the color was red (array index 10)










share|improve this question





















  • why not simply use the modulus operator instead? then you dont need to duplicate anything in the array.
    – Derek
    Nov 8 at 20:12






  • 1




    I was thinking since you only have red and black in alternate values on your array ad infinitum, why not assign red as odd and black as even and see if your random integer is odd or even. @Derek exactly!
    – I. R. R.
    Nov 8 at 20:12











  • I have one green at the end
    – Marchewka PL
    Nov 8 at 20:13






  • 1




    So if number can be divided by 15 - it means green
    – Artem Arkhipov
    Nov 8 at 20:14










  • I think you mix up things: if a color circle is 100px, why do you divide by 1500px to know which circle you are on? You should divide by 100. As you divide now, you will not get the color index, but how many times the whole pattern is repeated at the left of the circle.
    – trincot
    Nov 8 at 20:22














up vote
-1
down vote

favorite












I'm making a roulette using HTML/CSS/JS.
How it work: The roulette circles are a background-image that is made of 15 circles. 7red 7blue and 1green. The background-img is repeat-x so it never end. Then the javascript randomize a px position of the background img than the roulette image moves to that px. For an example if it randomize 17839, the roulette img will move to that point.
Now I'm trying to figure out how to tell the javascript on with color the roulette stoped.



My question is "How to loop an array(or copy) so it will have at least 2000 string Index of pattern below?"



I was trying to make a array:



var colors = ["red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "green"];


And then loop it somehow to make the array verry long. The max px that can be randomized is 20000. Every color circle is 100px. That make the whole patern 1500px long. That mean that I need a array that is at least 2000 string long. It feels wrong to write an array that long at my own so I'm trying to loop the color array to make it longer.



This is some example how I want it to work.



Random number is 1534.



random = 1534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 1, that mean that the color was red (array index 0)



Random number is 17534.



random = 17534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 11, that mean that the color was red (array index 10)










share|improve this question





















  • why not simply use the modulus operator instead? then you dont need to duplicate anything in the array.
    – Derek
    Nov 8 at 20:12






  • 1




    I was thinking since you only have red and black in alternate values on your array ad infinitum, why not assign red as odd and black as even and see if your random integer is odd or even. @Derek exactly!
    – I. R. R.
    Nov 8 at 20:12











  • I have one green at the end
    – Marchewka PL
    Nov 8 at 20:13






  • 1




    So if number can be divided by 15 - it means green
    – Artem Arkhipov
    Nov 8 at 20:14










  • I think you mix up things: if a color circle is 100px, why do you divide by 1500px to know which circle you are on? You should divide by 100. As you divide now, you will not get the color index, but how many times the whole pattern is repeated at the left of the circle.
    – trincot
    Nov 8 at 20:22












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm making a roulette using HTML/CSS/JS.
How it work: The roulette circles are a background-image that is made of 15 circles. 7red 7blue and 1green. The background-img is repeat-x so it never end. Then the javascript randomize a px position of the background img than the roulette image moves to that px. For an example if it randomize 17839, the roulette img will move to that point.
Now I'm trying to figure out how to tell the javascript on with color the roulette stoped.



My question is "How to loop an array(or copy) so it will have at least 2000 string Index of pattern below?"



I was trying to make a array:



var colors = ["red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "green"];


And then loop it somehow to make the array verry long. The max px that can be randomized is 20000. Every color circle is 100px. That make the whole patern 1500px long. That mean that I need a array that is at least 2000 string long. It feels wrong to write an array that long at my own so I'm trying to loop the color array to make it longer.



This is some example how I want it to work.



Random number is 1534.



random = 1534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 1, that mean that the color was red (array index 0)



Random number is 17534.



random = 17534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 11, that mean that the color was red (array index 10)










share|improve this question













I'm making a roulette using HTML/CSS/JS.
How it work: The roulette circles are a background-image that is made of 15 circles. 7red 7blue and 1green. The background-img is repeat-x so it never end. Then the javascript randomize a px position of the background img than the roulette image moves to that px. For an example if it randomize 17839, the roulette img will move to that point.
Now I'm trying to figure out how to tell the javascript on with color the roulette stoped.



My question is "How to loop an array(or copy) so it will have at least 2000 string Index of pattern below?"



I was trying to make a array:



var colors = ["red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "green"];


And then loop it somehow to make the array verry long. The max px that can be randomized is 20000. Every color circle is 100px. That make the whole patern 1500px long. That mean that I need a array that is at least 2000 string long. It feels wrong to write an array that long at my own so I'm trying to loop the color array to make it longer.



This is some example how I want it to work.



Random number is 1534.



random = 1534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 1, that mean that the color was red (array index 0)



Random number is 17534.



random = 17534; //randomized pixel number
position = Math.floor(random/1500); //dividing the random number with patern lenght
arrayConv = position - 1; //converting to an arrayIndex


The result will be 11, that mean that the color was red (array index 10)







javascript html css arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 20:09









Marchewka PL

113




113











  • why not simply use the modulus operator instead? then you dont need to duplicate anything in the array.
    – Derek
    Nov 8 at 20:12






  • 1




    I was thinking since you only have red and black in alternate values on your array ad infinitum, why not assign red as odd and black as even and see if your random integer is odd or even. @Derek exactly!
    – I. R. R.
    Nov 8 at 20:12











  • I have one green at the end
    – Marchewka PL
    Nov 8 at 20:13






  • 1




    So if number can be divided by 15 - it means green
    – Artem Arkhipov
    Nov 8 at 20:14










  • I think you mix up things: if a color circle is 100px, why do you divide by 1500px to know which circle you are on? You should divide by 100. As you divide now, you will not get the color index, but how many times the whole pattern is repeated at the left of the circle.
    – trincot
    Nov 8 at 20:22
















  • why not simply use the modulus operator instead? then you dont need to duplicate anything in the array.
    – Derek
    Nov 8 at 20:12






  • 1




    I was thinking since you only have red and black in alternate values on your array ad infinitum, why not assign red as odd and black as even and see if your random integer is odd or even. @Derek exactly!
    – I. R. R.
    Nov 8 at 20:12











  • I have one green at the end
    – Marchewka PL
    Nov 8 at 20:13






  • 1




    So if number can be divided by 15 - it means green
    – Artem Arkhipov
    Nov 8 at 20:14










  • I think you mix up things: if a color circle is 100px, why do you divide by 1500px to know which circle you are on? You should divide by 100. As you divide now, you will not get the color index, but how many times the whole pattern is repeated at the left of the circle.
    – trincot
    Nov 8 at 20:22















why not simply use the modulus operator instead? then you dont need to duplicate anything in the array.
– Derek
Nov 8 at 20:12




why not simply use the modulus operator instead? then you dont need to duplicate anything in the array.
– Derek
Nov 8 at 20:12




1




1




I was thinking since you only have red and black in alternate values on your array ad infinitum, why not assign red as odd and black as even and see if your random integer is odd or even. @Derek exactly!
– I. R. R.
Nov 8 at 20:12





I was thinking since you only have red and black in alternate values on your array ad infinitum, why not assign red as odd and black as even and see if your random integer is odd or even. @Derek exactly!
– I. R. R.
Nov 8 at 20:12













I have one green at the end
– Marchewka PL
Nov 8 at 20:13




I have one green at the end
– Marchewka PL
Nov 8 at 20:13




1




1




So if number can be divided by 15 - it means green
– Artem Arkhipov
Nov 8 at 20:14




So if number can be divided by 15 - it means green
– Artem Arkhipov
Nov 8 at 20:14












I think you mix up things: if a color circle is 100px, why do you divide by 1500px to know which circle you are on? You should divide by 100. As you divide now, you will not get the color index, but how many times the whole pattern is repeated at the left of the circle.
– trincot
Nov 8 at 20:22




I think you mix up things: if a color circle is 100px, why do you divide by 1500px to know which circle you are on? You should divide by 100. As you divide now, you will not get the color index, but how many times the whole pattern is repeated at the left of the circle.
– trincot
Nov 8 at 20:22












2 Answers
2






active

oldest

votes

















up vote
2
down vote













As you said there is 7 blue and 7 red blocks and 1 green in the end. So we can assume that blue/red color can depend on even/odd generated number is, except the case when number can be divided by 15 - in that case it means green.






function resolveColor(number) 
if (number % 15 === 0)
return 'green';

if (number % 2 === 0)
return 'red';

return 'blue';


const x = Math.ceil(Math.random()*20000);
console.log('Generated number: ', x);
console.log('Result is: ', resolveColor(x));








share|improve this answer






















  • Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
    – Marchewka PL
    Nov 8 at 23:45

















up vote
0
down vote













has previously discussed by I.r.r and Marchewka PL.There are three colours, And the result of random px calculated would always fall between 1 - 15. You can assign the red and blue as odd and even respectively and have the green colour to be a default; on a conditional statement.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215393%2fhow-to-loop-an-array-in-javascript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    As you said there is 7 blue and 7 red blocks and 1 green in the end. So we can assume that blue/red color can depend on even/odd generated number is, except the case when number can be divided by 15 - in that case it means green.






    function resolveColor(number) 
    if (number % 15 === 0)
    return 'green';

    if (number % 2 === 0)
    return 'red';

    return 'blue';


    const x = Math.ceil(Math.random()*20000);
    console.log('Generated number: ', x);
    console.log('Result is: ', resolveColor(x));








    share|improve this answer






















    • Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
      – Marchewka PL
      Nov 8 at 23:45














    up vote
    2
    down vote













    As you said there is 7 blue and 7 red blocks and 1 green in the end. So we can assume that blue/red color can depend on even/odd generated number is, except the case when number can be divided by 15 - in that case it means green.






    function resolveColor(number) 
    if (number % 15 === 0)
    return 'green';

    if (number % 2 === 0)
    return 'red';

    return 'blue';


    const x = Math.ceil(Math.random()*20000);
    console.log('Generated number: ', x);
    console.log('Result is: ', resolveColor(x));








    share|improve this answer






















    • Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
      – Marchewka PL
      Nov 8 at 23:45












    up vote
    2
    down vote










    up vote
    2
    down vote









    As you said there is 7 blue and 7 red blocks and 1 green in the end. So we can assume that blue/red color can depend on even/odd generated number is, except the case when number can be divided by 15 - in that case it means green.






    function resolveColor(number) 
    if (number % 15 === 0)
    return 'green';

    if (number % 2 === 0)
    return 'red';

    return 'blue';


    const x = Math.ceil(Math.random()*20000);
    console.log('Generated number: ', x);
    console.log('Result is: ', resolveColor(x));








    share|improve this answer














    As you said there is 7 blue and 7 red blocks and 1 green in the end. So we can assume that blue/red color can depend on even/odd generated number is, except the case when number can be divided by 15 - in that case it means green.






    function resolveColor(number) 
    if (number % 15 === 0)
    return 'green';

    if (number % 2 === 0)
    return 'red';

    return 'blue';


    const x = Math.ceil(Math.random()*20000);
    console.log('Generated number: ', x);
    console.log('Result is: ', resolveColor(x));








    function resolveColor(number) 
    if (number % 15 === 0)
    return 'green';

    if (number % 2 === 0)
    return 'red';

    return 'blue';


    const x = Math.ceil(Math.random()*20000);
    console.log('Generated number: ', x);
    console.log('Result is: ', resolveColor(x));





    function resolveColor(number) 
    if (number % 15 === 0)
    return 'green';

    if (number % 2 === 0)
    return 'red';

    return 'blue';


    const x = Math.ceil(Math.random()*20000);
    console.log('Generated number: ', x);
    console.log('Result is: ', resolveColor(x));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 8 at 20:34

























    answered Nov 8 at 20:29









    Artem Arkhipov

    1,305521




    1,305521











    • Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
      – Marchewka PL
      Nov 8 at 23:45
















    • Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
      – Marchewka PL
      Nov 8 at 23:45















    Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
    – Marchewka PL
    Nov 8 at 23:45




    Hello and thanks alot for your help. The idea is great but it seams not to work. It is because when the patern start over, the odd number is now even and even is now odd. i.imgur.com/PjrdHq1.png
    – Marchewka PL
    Nov 8 at 23:45












    up vote
    0
    down vote













    has previously discussed by I.r.r and Marchewka PL.There are three colours, And the result of random px calculated would always fall between 1 - 15. You can assign the red and blue as odd and even respectively and have the green colour to be a default; on a conditional statement.






    share|improve this answer


























      up vote
      0
      down vote













      has previously discussed by I.r.r and Marchewka PL.There are three colours, And the result of random px calculated would always fall between 1 - 15. You can assign the red and blue as odd and even respectively and have the green colour to be a default; on a conditional statement.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        has previously discussed by I.r.r and Marchewka PL.There are three colours, And the result of random px calculated would always fall between 1 - 15. You can assign the red and blue as odd and even respectively and have the green colour to be a default; on a conditional statement.






        share|improve this answer














        has previously discussed by I.r.r and Marchewka PL.There are three colours, And the result of random px calculated would always fall between 1 - 15. You can assign the red and blue as odd and even respectively and have the green colour to be a default; on a conditional statement.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 20:58

























        answered Nov 8 at 20:49









        akolliy

        145




        145



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215393%2fhow-to-loop-an-array-in-javascript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Crossroads (UK TV series)

            ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế