Solved

Javascript Index Array Variable

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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).

735 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