4 Replies - 606 Views - Last Post: 28 September 2012 - 04:30 AM

#1 Arceopteryx  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 40
  • Joined: 14-September 07

Array spelling mistake

Posted 27 September 2012 - 05:07 AM

Hello!

I was doing a website project for school, and I needed to do a simple image gallery. Very simple.
So I made an array to contain the paths for the images.

The code for the entire gallery looks like this:

function display(i){
	var pictures = new Array();
	pictures[0] = "images/when.png";
	pictures[1] = "images/moody.jpg";
	pictures[2] = "images/thousand.jpg";
	pictures[3] = "images/morning.jpg";
	pictures[4] = "images/now.jpg";
	pictures[5] = "images/children.jpg";
	pictures[6] = "images/free.jpg";
	pictures[7] = "images/igtm.png";
	pictures[8] = "images/sworcery.jpg";
	pictures[9] = "images/scythian-steppes.jpg";
	
	document.getElementById("album").setAttribute("src", pictures[i]);
}


This is the correct version, that works. However before this, I had made a typo on the last line of the array, so instead it said:

pitcures[9] = "images/scythian-steppes.jpg";


If I commented part of the array out, that contained this error, then the onclick in the html would work. If it wasn't commented out, it didn't work, and wouldn't change any of the pictures, even from the ones that were correctly written.

So I got a couple of questions.

1. Why did the rest of the pictures before that not show up? (aka. The last line in the array broke everything)
2. Dreamweaver didn't highlight it. Did it see it as a declaration?
3. Is there a way to avoid this in the future. If I would be doing a script on 100 lines or more, it would takes ages to find that error again.

Thank you for your time!

TL;DR I want better code highlighting in dreamweaver.

Is This A Good Question/Topic? 0
  • +

Replies To: Array spelling mistake

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Array spelling mistake

Posted 27 September 2012 - 06:30 AM

View PostArceopteryx, on 27 September 2012 - 01:07 PM, said:

1. Why did the rest of the pictures before that not show up? (aka. The last line in the array broke everything)

Because your script had a compilation error in it and so none of it would run past this.

View PostArceopteryx, on 27 September 2012 - 01:07 PM, said:

2. Dreamweaver didn't highlight it. Did it see it as a declaration?

Dreamweaver isn't suitable for web development, don't use it.

View PostArceopteryx, on 27 September 2012 - 01:07 PM, said:

3. Is there a way to avoid this in the future. If I would be doing a script on 100 lines or more, it would takes ages to find that error again.

It wouldn't, use incremental testing. Make a change, test, make a change, test, make a change, test.

Or of course, just look in your browser's error log for where the compilation error occurred.
Was This Post Helpful? 2
  • +
  • -

#3 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: Array spelling mistake

Posted 27 September 2012 - 07:04 AM

look at using JsLint. what JsLint will do is parse your Javascript and highlight errors and bad practices. In this case it would have alerted you that your misspelled variable was being used before it was declared with a "var" statement.

Aptana and some other IDE's for web development also have live linting built in so it will tell you if you've done something wrong in real time.
Was This Post Helpful? 1
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Array spelling mistake

Posted 27 September 2012 - 07:05 AM

besides that you can use the array literal, which circumvents that problem completely.
var pictures = [
    "value 1",
    "value 2",
    "value 3"
];

Was This Post Helpful? 1
  • +
  • -

#5 Arceopteryx  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 40
  • Joined: 14-September 07

Re: Array spelling mistake

Posted 28 September 2012 - 04:30 AM

Thank you for your responses and the time you took off to reply. It was exactly what I needed. I'll follow your advice! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1