Solved

Javascript Index Array Variable

Posted on 2013-10-23
1
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

636 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