![]() |
Let's make with the programming
|
Watch for more tomorrow. Yesterday I worked until 1:30am, and then this morning my employer called me and got me out of bed to come back into the office on urgent business. Spare time is, at the moment, a luxury I don't have.
Sorry about the delay. |
Ok, sorry about that delay. Due to over-exposure to work, I was clinically dead for a while. But it went away.
Quote:
Quote:
More soon. |
I think I'm still with you, although it did get a little fuzzy wif objects n cars n stuff.. but so far so good.
|
This would be a good time to mention variable scope. When you create a variable in your script with the var keyword, that is where the variable is defined. If the variable is defined inside a block, it's scope is limited to that block. What that means is, once you leave the block of code which defined the variable, it goes away, and no code outside of that block can see the variable. However, any sub-blocks of the block which contains the variable definition can access it, no matter how deep the sub-block is.
Any variable defined outside of a block is accessible to everything, because it is in the assumed block which wraps around everything, and every other block is a sub-block of that one. Such a variable is called a "global" variable, since everything can see it. Here's a chunk of code to illustrate... the numbers are there as a reference, not part of the code itself: Code:
00 var jimIQ = 135; If I tried to accecss jimResponse , notJimResponse or iqResponse at any point after line 15, I'd get a "variable not defined" error, because their blocks ended, and the variables went away. Next: Functions. Then Operators. Then conditionals and loops. Unless I think of something else that I ought to cover first. The way scope works, and it's usefulness, will become more obvious when we get to Functions and Loops. |
Quote:
|
Can you reference line numbers as you could in the old basic programming language? For instance "Goto 20"?
|
Quote:
I'll try to post the next bit this afternoon, if I get a break in work... |
I tried to post some javascript for a random sound generator, but it was apparantly incorporated into the page so you couldn't see anything (or hear anything because the sounds aren't here)
|
Quote:
I hope to post more here later today, assuming my brain receovers. I was here at work until 5:30 in the morning, and now I'm back again, so I'm a bit fried at this very moment. |
Ok... the gaping maw of work which recently swallowed me up has finally vomited me back out again. Look for more here soon.
|
yeah, you keep saying that, you big tease. why should we trust you this time?
|
Ok, on to Functions. If you want to create a function, you use the function keyword. Like with var, the function keyword is then followed by the function name. Then, you include a list of arguments in parethases (we'll get to this in a bit), and then the block of code which will execute when a call is made to this function.
Here's a simple example, using some code from a previous example: Code:
function changeBackgroundColor () { Code:
changeBackgroundColor(); Now let's modify it to accept arguments. When we create the function with the function keyword, if we define arguments in the parenthases, then we can pass information to the function for it to work with. Here's an example: Code:
function changeBackgroundColor (prompt, newColor) { Code:
var colorPrompt = "Click OK to change the background color.\nClick Cancel to leave it unchanged."; Code:
changeBackgroundColor ("Do you want to change the background color?", "green"); Code:
function addTwoNumbers (first, second) { Code:
var someNumber = prompt("Enter any number"); Suppose the user leaves the entry fields blank when they run this script. The prompt() function would return an empty string (""), and the call to addTwoNumbers() would fail, because there are no numbers to add. If we wanted to be cautious in our programming, and make sure the user didn't provide an empty string, we can test for that in the function, and provide an error if necessary: Code:
function addTwoNumbers (first, second) { More later... |
Quote:
|
I'm not seeing any questions from people trying to clarify what I've written... does that mean that I'm doing such a por job of explaining that no one knows what to ask, or am I doing such a wonderful job of explaining that there are no unanswered questions yet?
Or is it just that no one is reading this thread anymore? Hello? Echo echo echo... |
All times are GMT -5. The time now is 10:06 PM. |
Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.