JavaScript arrays

Hello expert,

Doing some basic investigations using JavaScript Arrays
in the script below after I got the array to write

Open in new window

to the page
I was going to get the index number to write. Added

var y = myColors.indexOf("green");

but this breaks the code.

I have checked the syntax and it seems to be ok.

What anm I doing wrong?

Thanks

Allen in Dallas
JS-array-loop.html
Allen PittsBusiness analystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
What do you mean by 'breaks the code'

In your page you are calling this:

var y = myColors.indexOf("green");

before you ever setup the myColors array, so it will always return false (-1)
0
Allen PittsBusiness analystAuthor Commented:
Hello Chris,

Before the page wrote out s list of colors. When I included the indexOf statement
the page was blank.

So the index of statement was moved to after the  array (attachwed file) variable but
the same result, a blank page, is achieved.

Allen
JS-array-loop.html
0
duncanb7Commented:
What you mean  you get blank page ? could you explain it more ?

I tried you both html code,

I got this

before indexOf is removed to after the array defined
 
blue
green
red
purple
yellow
orange
aqua
mauve-1

Open in new window

after indexOf is removed to after the array defined
blue
green
red
purple
yellow
orange
aqua
mauve1

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Chris StanyonWebDevCommented:
Both the files you've attached do exactly what they're supposed to - they write out a list of colours, followed by 'mauve'. The first file then writes out -1 because you call the indexOf before setting the array, while the scond file writes out 1, which is correct - 'green' is at position 1.

Don't see a problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
duncanb7Commented:
I agree Christ I run it at my side and I can output the result as Chris said

Is it browser issue,  9apit ? What version of your older browser you are using ?
and it might be related to HTML/XHTML' s DOCTYPE for closing tag issue
that I am not sure without your more information about your browsers

Duncan
0
Allen PittsBusiness analystAuthor Commented:
Hello Chris,

When i tried it in FireFox it performed just as stated.

Still get blank page in IE 8.
Thought it might be JavaScript turned off but I checked
Internet Options, Secuity and JavaScript is not disabled.

Besides when I comment out the indexOf  statement
the rest of the code works, so JavaScript would have to be
working for the rest of the code to work.
When I uncomment out the
var y =... line the page goes blank again. The IE 8 browser
is on a lap top at a corporation where they have really
locked down the security. So it maybe something the
IT dept, did to restrict script access. For instance,
when an IE session is first begun before the page displays
a message appears: "To help protect your security
Internet Explorer has restricted this web page from running script
or Active X controls that could access to your computer. Click
here for options. Then when the link is clicked a second warning is given.
But even when the browser is told twice that scripting is ok the
page returns blank.

Thanks.

Allen in Dallas
0
duncanb7Commented:
Dear 9apit,

Christ found the result already but the issue is not related
to your modified post code, JS-array-loop.html, and you said you get blank page(or not working) after Chris correcting your code.

Is it related to browser ? Am I right ?

Please advise

Duncan
0
Allen PittsBusiness analystAuthor Commented:
Hello Duncan,

Yes it is browser related.
The code works in FireFox
but not in the IE 8 installed on
my corporate laptop.

Allen
0
duncanb7Commented:
thanks you agree that is related to browser issue and not just code .


Duncan
0
Chris StanyonWebDevCommented:
Hey 9apit - now you've said that you're using IE8 (an out of date browser), it makes more sense. The indexOf function is not supported in IE8, so it causes an error. IE is very unforgiving with Javascript errors, and a single error will cause your whole script to fail.

Couple of options. If it's just the index you need, then you already have that in the i variable inside the loop. Another option is to define your own indexOf function. Havea read here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

The third option (and my favourite) is to start using jQuery. It may sometimes seem like overkill to include the library, particularly if you have very simple javascript requirements, but the footprint is so small, and it solves all these cross-browser issues. To loop through your array:

$.each(myColors, function(i, color) {
    console.log(color);
});

Open in new window

To get an index of an array element

alert($.inArray('yellow',myColors));

Open in new window

@duncan - you didn't give any answers - you simply asked what browser version. You also said it was to do with DOCTYPES and closing tags!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.