Solved

JavaScript arrays

Posted on 2013-11-19
11
260 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:9apit
  • 4
  • 3
  • 3
11 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
 

Author Comment

by:9apit
Comment Utility
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
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
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
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Closing Comment

by:9apit
Comment Utility
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
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
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
 

Author Comment

by:9apit
Comment Utility
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
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
thanks you agree that is related to browser issue and not just code .


Duncan
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now