Solved

Javascript Index Array Variable

Posted on 2013-10-23
1
450 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

759 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

21 Experts available now in Live!

Get 1:1 Help Now