The problem now, is that whenever I try adding a block of text to the page that describes something that, ironically, is about error messages, the page just displays "Error" whenever I load it up. However, if I delete a particular line from this paragraph, the page loads as normal. The paragraph where this is happening is enclosed by simple <p></p>, and has no scripts or other HTML in it and is otherwise not tied to any other block of code on the page at all (well, maybe not entirely, see below). Even putting the line in its own paragraph does not solve the problem, and it's a line that I kind of need in the description for it to make sense; it's a sentence about not defining the script as "text/javascript" in the <script> tag, but even when I break it up and make it as uncode-like as possible, it still returns "Error". It seems that if the word "script" appears in the sentence, the page fails (I had to spell it as s-cript for it to not break the page), and even having parenthesis in the paragraph block does it too. I've no idea how or why this is causing problems.
A pretty important thing to remember is that your javascript (the stuff that (usually) goes between your <script> tags) and the html is really separate entities. If the particular line of code that seems to be breaking things is containing the string <script>, the web page will interpret
the rest of the page, all the way until it reaches </script>, as javascript, not html. Since what you've got right after <script> starts probably doesn't contain something that's formatted in legal javascript, you get javascript errors, as the interpretation fails. That could be it, I'd have to look at your code to be sure (if you put it in a google doc? Or just post a link to it somewhere?), but it's the only explanation I can come up with.
for(var i = cars.length-1; i >= 0; i--) {
}
I do'nt know enough about Java to say for sure, but your code made me wince.
While I would imagine i is signed, your code fails spectacularly if it's unsigned. Maybe that's more of a C concern, though.
Of course, you probably knew that.
javascript is NOT java*. At all. It has
no signage, whatsoever, just a var statement that does nothing but limiting the scope of the variable. You can do this:
var s = "string";
console.log(s); //prints string
s = 1;
console.log(s); //prints 1
See? A variable isn't tied to any kind of type whatsoever. You will get problems when trying to do stuff with different types, you can't go "hello" / 4. The classes and objects are also a lot less strict what you have in java/c++, you can just add properties and methods to an object at will. An object is kinda just a map from variable names to whatever. var object = {};
is an object instantiation. Blew my mind when I first ran into it. Javascript is weird around the edges, check out the later half of
this video (check the first part out too, the ruby part is also funny)
The var statement is important, though. Amongst other things, it prevents this fun little thing:
var someArr = new Array(2);
someArr [0] = "hellu";
someArr [1] = "hella";
function swap(arr, ind1, ind2) {
temp = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = temp;
};
swap(someArr, 0, 1);
console.log(temp); //WILL log "hellu". I am NOT kidding. If i had written "var temp" over, it would throw an undefined error, as you'd expect from any other language.
It is
horrible at start, but you get used to it. If you tried to do something like that in java, you'd get compilation errors right away. Think of java as c++ without having to deal with pointer arithmetic or segfaults and the other things that makes c/c++ a complete fucking mess. Javascript is a lot more lenient than anything else - basically the idea is that it shouldn't fail on type errors, since it has to deal with user input to such a degree that it would crash all the times otherwise. I'm not sure if I agree with that philosophy, but you have to live with it.
* I don't quite remember why javascript is named javascript, but I've heard it said that around when javascript first popped up, java was kinda hip, so they just grabbed the name.