Anyone here good with javascript?

I'm having trouble with something seemingly trivial and thought I'd ask here first before heading over to a different community. I lyke u gaiz
Don't ask to ask, ask first, and if nobody knows the answer, then you should head somewhere else :)
The problem:
I'm parsing the input file and loading it into a simple structure. The file contains labels of types of locations, and coordinates of various examples of said locations. The parser starts by checking if the first character of the line is a '#'. If so, it uses the tail of the line to push a new instance of a Location object into an array with the tail as it's label. If not, it passes the line to the coordinate function, which either appends a set of coordinates to the appropriate member of the top element of the Locations array, or returns an integer that is keeping track of the top of the array. That integer is the variable i. And after 3 lines have been parsed it becomes undefined.
For the life of me I can't figure out why. Testing it in chrome.

the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<html>
	<head>
		<input type="file" id="waypoints" name="files" />
		<script type="text/javascript">
			if (window.File && window.FileReader && window.FileList && window.Blob) {
				
				// Listed in order of precedence
				var token = new Array();
				token[0] = "#";
				token[1] = ":";

				
				var locations = new Array();


				function Location(label) {
					this.label = label;
					this.coordinates = new Array();
					
				}

				function coordinate(line, i) {
					var ch = line.charAt(0);
					console.log(line);
					if (ch == token[1]) {
					  console.log(i);
						locations[i].coordinates.push(line.slice(1))
						
					}
					else return i;
				}

				function label(line, i) {
					ch = line.charAt(0)
					if (ch == token[0]) {
						locations.push(new Location(line.slice(1)));
						console.log(locations);
						if(i == 0) return 0;
						return i++;
					} else return coordinate(line, i);
				}
				
				function handleFileSelect(evt) {
					var files = evt.target.files;
						var f = files[0];
						var fr = new FileReader();
						fr.addEventListener("load", function(event) {
							var text = fr.result;
						
							var lines = text.split(/[\r\n]+/g);
							console.log(lines);
						  var i = 0;
							lines.forEach(function(line) {
									i = label(line, i);
							});
							displayContents(locations);
						});
						fr.readAsText(f);
					}
				
					
				
				function displayContents(locations) {
					locations.forEach(function(index) {
				
						document.getElementById("content").innerHTML = "<table><tr><th>"
										  + index.label
										  + "</th></tr>";
					index.coordinates.forEach(function(coord) {
									    document.getElementById("content").innerHTML += "<td>" + coord + "</td>";
						});
						document.getElementById("content").innerHTML += "</table>";
					});
				}
				
				//main
				window.onload = function() {
					document.getElementById('waypoints').addEventListener('change', handleFileSelect, false);
				};
			}
			else alert('The File APIs are not fully supported by your browser.');
		</script>
	</head>
	<body>
		<output id=content></output>
	</body>
</html>


THe input file:
#HOME
:-240, 64, 262
:-399, 198, 64

#CAVE
:-343, -203, 81
:-452, -465, 61

#rubber tree
:-414, -539, 64

#canyon
:-294, 233, 64

#sheeeeeeeep
:-382, -62, 68
:-529, 399, 65
EDIT: changing line 27 to
locations[locations.length-1] fixed the issue, getting rid of i. Too used to programming in C and lisp. New to javascript. Now I just gotta get formatting right :P
Last edited on
Topic archived. No new replies allowed.