![]() |
|
Technology Computing, programming, science, electronics, telecommunications, etc. |
![]() |
|
Thread Tools | Display Modes |
![]() |
#31 |
Constitutional Scholar
Join Date: Dec 2002
Location: Ocala, FL
Posts: 4,006
|
Let's make with the programming
__________________
"I'm completely in favor of the separation of Church and State. My idea is that these two institutions screw us up enough on their own, so both of them together is certain death." - George Carlin |
![]() |
![]() |
![]() |
#32 |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
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.
__________________
Hot Pastrami! |
![]() |
![]() |
![]() |
#33 | ||
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
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.
__________________
Hot Pastrami! |
||
![]() |
![]() |
![]() |
#34 |
Junior Master Dwellar
Join Date: Mar 2003
Location: Kingdom of Atlantia
Posts: 2,979
|
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.
|
![]() |
![]() |
![]() |
#35 |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
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; 01 var userName = prompt("Please enter your name"); 02 var userIQ = prompt("Please enter your IQ"); 03 04 if (userName == "Lumberjim") { 05 var jimResponse = "You're too sexy for your shirt"; 06 document.write(response); 07 } 08 else { 09 var notJimResponse = "Jim is too sexy for his shirt"; 10 document.write(notJimResponse); 11 if (userIQ < jimIQ) { 12 var iqResponse = " ...and you're not as smart as he is."; 13 document.write(iqResponse); 14 } 15 } 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.
__________________
Hot Pastrami! |
![]() |
![]() |
![]() |
#36 | |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
Quote:
__________________
Hot Pastrami! |
|
![]() |
![]() |
![]() |
#37 |
Constitutional Scholar
Join Date: Dec 2002
Location: Ocala, FL
Posts: 4,006
|
Can you reference line numbers as you could in the old basic programming language? For instance "Goto 20"?
__________________
"I'm completely in favor of the separation of Church and State. My idea is that these two institutions screw us up enough on their own, so both of them together is certain death." - George Carlin |
![]() |
![]() |
![]() |
#38 | |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
Quote:
I'll try to post the next bit this afternoon, if I get a break in work...
__________________
Hot Pastrami! |
|
![]() |
![]() |
![]() |
#39 |
Constitutional Scholar
Join Date: Dec 2002
Location: Ocala, FL
Posts: 4,006
|
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)
__________________
"I'm completely in favor of the separation of Church and State. My idea is that these two institutions screw us up enough on their own, so both of them together is certain death." - George Carlin Last edited by Radar; 03-30-2004 at 01:23 PM. |
![]() |
![]() |
![]() |
#40 | |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
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.
__________________
Hot Pastrami! |
|
![]() |
![]() |
![]() |
#41 |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
Ok... the gaping maw of work which recently swallowed me up has finally vomited me back out again. Look for more here soon.
__________________
Hot Pastrami! |
![]() |
![]() |
![]() |
#42 |
I can hear my ears
Join Date: Oct 2003
Posts: 25,571
|
yeah, you keep saying that, you big tease. why should we trust you this time?
__________________
This body holding me reminds me of my own mortality Embrace this moment, remember We are eternal, all this pain is an illusion ~MJKeenan |
![]() |
![]() |
![]() |
#43 |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
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 () { var prompt = "Click OK to change the background to red.\nClick Cancel to make it blue."; if (confirm(prompt)) { document.bgColor = "red"; } else { alert("We'll make it blue then."); document.bgColor = "blue"; } } 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) { if (confirm(prompt)) { document.bgColor = newColor; } } Code:
var colorPrompt = "Click OK to change the background color.\nClick Cancel to leave it unchanged."; var color = "purple"; changeBackgroundColor(prompt, color); Code:
changeBackgroundColor ("Do you want to change the background color?", "green"); Code:
function addTwoNumbers (first, second) { var sum = parseInt(first) + parseInt(second); return sum; } Code:
var someNumber = prompt("Enter any number"); var anotherNumber = prompt("Enter any other number"); var myTotal = addTwoNumbers(someNumber, anotherNumber); alert(myTotal); 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) { if (first == "") { alert("You didn't provide the first number"); return 0; } if (second == "") { alert("You didn't provide the second number"); return 0; } var sum = parseInt(first) + parseInt(second); return sum; } More later...
__________________
Hot Pastrami! |
![]() |
![]() |
![]() |
#44 | |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
Quote:
__________________
Hot Pastrami! |
|
![]() |
![]() |
![]() |
#45 |
I am meaty
Join Date: Dec 2001
Location: Salt Lake City, UT
Posts: 1,119
|
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...
__________________
Hot Pastrami! |
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | |
|
|