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)
javascript html css arrays
add a comment |
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)
javascript html css arrays
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
add a comment |
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)
javascript html css arrays
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
javascript html css arrays
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
add a comment |
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
add a comment |
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));
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
add a comment |
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.
add a comment |
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));
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
add a comment |
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));
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
add a comment |
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));
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));
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 8 at 20:58
answered Nov 8 at 20:49
akolliy
145
145
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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