var i = 0;
var cars = new Array();
cars[0] = "Saab";
cars[1] = "Volvo";
cars[2] = "BMW";
while(i < cars.length) {
document.write(cars[i] + "<*br>");
i++;
}
var persons = new Array();
var tom = new Array();
tom[0] = "Tom";
tom[1] = "www.Tom.com";
tom[2] = "Lasanga";
var carl = new Array();
carl[0] = "Carl";
carl[1] = "www.Carl.com";
carl[2] = "Pizza";
var tina = new Array();
tina[0] = "Tina";
tina[1] = "www.Tina.com";
tina[2] = "The Blood of the innocent";
persons[0] = tom;
persons[1] = carl;
persons[2] = tina;
for(var i = 0; i < persons.length; i++) {
console.log("Person number " + (i+1) + ":");
console.log("Name: " + persons[i][0]);
console.log("Web Page: " + persons[i][1]);
console.log("Favourite Dish: " + persons[i][2]);
console.log("\n"); //New line
}
i++;
i += 1;
i = i+1;
Ok, so it's safe to assume that i=0 by default all the time then?
for(var i = cars.length-1; i >= 0; i--) {
}
console.log(i);
var i;
console.log(i);
i = 5;
console.log(i);
i = 5;
console.log(i);
There is no reason other than convention to use the variable name i in the loop.
Code: [Select]for(var i = cars.length-1; i >= 0; i--) {
}
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.
Code: [Select]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.(click to show/hide)
var s = "string";
console.log(s); //prints string
s = 1;
console.log(s); //prints 1
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.
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.
<p> This is some text that talks about the <script> tag! </p>
<h1> This is not written to the page, it stops after "the" </h1>
<script>
</script>
<p> This is some text that talks about the <script> tag! </p>
<h1> This is not written to the page, it stops after "the" </h1>
Have you closed your <h6> with a </h6>?No, it's not actually written out, but is a do...while loop. Is written like this:
<script type="text/javascript">
i = 1
do {
document.write("<h" + i + ">This is heading " + i);
document.write("<h" + i + ">");
i++;
}
while (i <= 6)
</script>
In addition to what pwhodges said, do you use an assisted editor integrated development environment? Like, one that points out when you don't close brackets or finish statements and such?I'm not...do you know of a good one? I'm mainly using Notepad and the W3 example pages.
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....But there's no <script> tag in the paragraph, just the word "script", no markers or anything around 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.That's the story I heard too. Along with something about having computer languages share similar names to organize them or something. Yeah, didn't quite work out that way...
QuoteIn addition to what pwhodges said, do you use an assisted editor integrated development environment? Like, one that points out when you don't close brackets or finish statements and such?I'm not...do you know of a good one? I'm mainly using Notepad and the W3 example pages.
I'd suggest at least upgrading to notepad++ if you're on windows. It's not an IDE per se, but it has a bit of syntax highlighting for javascript/html.I'll check it out. I miss way too many closing / ' " ] etc...I'm surprised I haven't started pulling my hair out yet.
I always turn that feature off because I end up not noticing that it closed the bracket, and I close it again.
a = b + c
(d+e).print()
a = b + c(d+e).print()
private boolean isMasterpieceMakingSense{ // oh yeah, method declarations also don't end with semicolons, but they're also a group of statements anyway...
if(!isMasterpieceTalkingBonkers && isCodeExampleCorrect){ // start of a group of statements, no semicolon
logging.put("Heck yeah, I made it!"); // semicolon after an expression!
return true; // semicolon after an expression!
}else{ // no semicolon after the first bracket, and none after the else statement
logging.put("Awww, bonkers."); // another expression
return false; // last expression
}
} // end of method declaration, no semicolon.
Why did you name a method isTalkingBonkers if your returning false when you ARE talking bonkers?
More importantly, why is isMasterpieceMakingSense not a function? :-D
More importantly, why is isMasterpieceMakingSense not a function? :-D
function isThisAFunction(){}
document.write(typeof isThisAFunction+"<br>");
if (isThisAFunction) {
document.write("isThisAFunction evaluates to true when passed to an if, because the if checks for existence"+"<br>");
}
else
{
document.write("isThisAFunction evaluates to false"+"<br>");
}
if (isThisAFunction==true) {
document.write("isThisAFunction is the same as true"+"<br>");
}
else
{
document.write("isThisAFunction is, however, not the same as true, even by lax comparison"+"<br>");
}
isThisAFunction = 5;
document.write(typeof isThisAFunction+"<br>");
function
isThisAFunction evaluates to true when passed to an if, because if checks for existence
isThisAFunction is, however, not the same as true, even by lax comparison
number
<button onclick="timedCount()">Start count</button>
<input type="button" value="Start count" onClick="timedCount()" />
Both point to the same function when placed in the same block of code, but for some reason, only the top one works (this is for a basic timer; text of the function is the same for both attempts). The second one creates a button, but its nonfunctioning. I've had this happen several other times, and each time when I went back and changed the code from the second format to the first's, then it works fine. What causes one to work and the other to not?Ah ok. Gonna keep the ;'s then, since I'm still getting used to the code.
Ok, here's another thing I've run into, and it took some hair pulling and whatnot to figure out. Here's code for two buttons:Code: [Select]<button onclick="timedCount()">Start count</button>
Both point to the same function when placed in the same block of code, but for some reason, only the top one works (this is for a basic timer; text of the function is the same for both attempts). The second one creates a button, but its nonfunctioning. I've had this happen several other times, and each time when I went back and changed the code from the second format to the first's, then it works fine. What causes one to work and the other to not?
<input type="button" value="Start count" onClick="timedCount()" />
<html>
<button onclick="timedCount(this)">Start count</button>
<input type="button" value="Start count" onClick="timedCount(this)" />
<script>
function timedCount(btn) {
console.log("pressed from: " + btn);
}
</script>
</html>
Get rid of the / after the onclick code, don't need that.
You are wrong on the / not being required, it closes off the input tag... but it doesn't seem to matter if it's there or not.
but chrome handles both onclick and onClick in both the button and the input.Strange, for lack of proper time to test I used the developer console to change onclick to onClick and the onClick and it's variable disappears.
Sorflakne, can you post your code? It'd help to be able to look at it.I'm trying to remember which one it was (I've made several...serves me right for not giving each a unique name), but if I do, I'll post it.
Other than that I cannot see what the problem could be, are you using anything to debug the Javascript? Even Chrome comes with a developer console, very handy.I'm going to the console in Firefox and selecting Debug Mode, but nothing happens. It displays the page code, but there's no indicators for stuff that's wrong, no way to manipulate text or whatnot...not really sure how to use it.
There comes a point, eventually (trust me), when you "get" the concepts that vary between languages in such a way that you can see them all as restricted dialects of one greater "computer language".^this