Solved

Javascript Index Array Variable

Posted on 2013-10-23
1
468 Views
Last Modified: 2013-11-19
Hello expert,

Thanks to COBOL Dinasaur for helping me to get the JavaScript to work.
Code attached.

These are statements I believe to be true. Correction or comments
solicited.

In the line right after the array is listed is the statement "i=0;". Was not sure
what this was for. So I removed it and the script broke. I thought it was defining
the variable i. So it tried   "var i=0;" and it still worked. So my surmise is "i=0;" defines the index variable i and sets it to zero.
So I tried "i=2; and the screen started red and then went to yellow.
So the line
 i=(i>=colors.length) ? 0 : i+1;
is taking the array index number and incrementing it. It starts with zero (red),
then takes the index variable i, which is set to two (green)  and kicks it up a
notch to three (yellow). Thats why with i set to two the screen goes red, yellow.
Also the reason it always starts with red is the script is set to do that with
document.body.style.backgroundColor = colors[0];
This was proved by changing the array number from zero to two which makes the screen
begin green.

Question

I thought that the script would loop thru the colors and stop. The reason
it does not stop is the conditional statement    
 i= (i>=colors.length) ? 0 : i+1;
says if the index var (i) is greater than the length of the array then
set the index var back to zero If the index var is not greater
than the length of the array then increment the index var.

Actually I think I just proved this is true. If the conditional is
removed and  
i= (i>=colors.length) ? 0 : i+1;
is substituted with
i = i+1
then the screen starts with red loops thru the colors and stops with
purple.

Ok. So I answered my own question. Often it is by thinking about
a system long enough to ask a logical question that the answer
is discovered. If you have any comments I am still interested.

Thanks again.

Allen in Dallas
change-k.html
0
Comment
Question by:9apit
1 Comment
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 500 total points
ID: 39596429
lol, why ask the question if you already answered it yourself. You should request a delete and refund of this question.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now