The Cellar

The Cellar (http://cellar.org/index.php)
-   Technology (http://cellar.org/forumdisplay.php?f=7)
-   -   Learn Javascript (http://cellar.org/showthread.php?t=5389)

lumberjim 04-06-2004 03:55 PM

it's been so long since i read the first part, i'll have to start over from the beginning. Based on your prior descriptions, I'm sure the last one was fine....I'll catch up.

hot_pastrami 04-06-2004 04:23 PM

OPERATORS

An operator in programming is like an operator in Math... add (+), subtract (-), multiply (*), divide (/)... etc. There are lots of them in programming. I'll cover the basic ones here, and we may run into others as we go along.


For numbers, operator behavior is exactly as expected. These are called Arithmetic Operators:

Code:

alert(100 + 200); //add
alert(300 - 32);  //subtract
alert(5 * 5); //multiply
alert(20 / 2); //divide

There is also the Assignment operator, the plain old equals sign, which assigns a value to a variable. We've used this one many times in our examples:

Code:

var someNumber = 10 + 3;
Just like in arithmetic, you can use parenthases to force things to happen in a certain order. If you don't use parenthasees, everything happens in the same order you learned in math class (Multiply, Divide, Add, Subtract):

Code:

var attendees = 40;
var extraSeats = 5;
var sessions = 10;

var totalMeals = (attendees + extraSeats) * sessions; // result will be 450
var badMath = attendees + extraSeats * sessions;  // result will be 90

The + operator is special in a way, because it works on strings as well as on numbers. If you add two strings together, the just get stuck together into one string. This is useful for lots of stuff:

Code:

function getUserName () {
  var name = prompt("Enter your full name");
  return name;
}

function welcomeUser (name) {
  document.write("Welcome to this website " + name + "!");
}

var userName = getUserName();
welcomeUser(userName);

There are also operators to compare things, as we've seen a little in our examples with the == operator. These are useful in conditionals, like the if statement, and some others that I'll post soon. Here are the most common ones:

Code:

Two items are equal: ==
Two items are NOT equal: !=
First item is greater in value to the second item: >
First item is lesser in value to the second item: <
First item is greater than or equal to the second item: >=
First item is less than or equal to the second item: <=

We'll use these in future examples.

The last kind of operator to cover right now are Logical operators. There is the Logical AND operator (&&) and the Logical OR (||) operator. These allow you to have two or more conditions inside a conditional statement. With AND, both conditionals must test TRUE in order to execute the conditional's block. With OR, at least one of the conditionals must test TRUE.

Code:

var firstNumber = 900;
var secondNumber = 150;

if (firstNumber > secondNumber && firstNumber == 1000) {
  alert("This message will not appear unless you change firstNumber to 1000.");
  alert("...because though the first conditional is true, the second is not.");
}

if (secondNumber == 100 || secondNumber / 50 == 3) {
  alert("This WILL appear");
  alert("...because though the first conditional failed, the second was true");
  alert("...which enough for an OR");
}

The other Logical Operator is the Logical NOT, which is the exclamation point (!). It will reverse the results of any conditional. We'll use that one later.

hot_pastrami 04-06-2004 04:26 PM

By the way... everyone who is currently paying attention to this thread, please pipe up and let me know. If Jimbo is the only one reading it at this point, then it's probably not worth posting it all publicly.

I just want to make certain that it's worthwhile to type all this up.

Radar 04-07-2004 04:38 PM

I'm looking at it between schooling criminals who want to rob me and everyone else in America. ;)

SteveDallas 04-09-2004 11:00 PM

a conundrum for the class
 
I received this little ditty in the mail. Just for my own perverse interest, is there any good way to figure out what the output is without actually running it? (I have replaced '< >' with '{ }' for obvious reasons.)

{script language="JavaScript"}
expedition = new Array(47,
56,61,35,3,66,8,80,134,98,140,
44,91,118,210,214,122,190,180,244,82,
166,73,122,91,249,157,191,48,42,16,
64,144,213,71,218,42,174,199,132,87,
174,14,232,51,84,238,123,153,133,164,
124,10,46,117,9,52,148,190,68,10,
134,46,132,149,32,57,62,31,30,251,
91,107,96,14,178,7,147,52,32,143,
108,89,237,87,41,21,131,104,120,132,
177,55,55,207,47,185,36,168,34,67,
176,133,45,110,193,102,14,84,20,108,
227,145,128,9,81,170,130,81,121,59,
249,223,178,215,44,140,209,179,159,135,
139,207,179,195,250,95,160,78,245,162,
180,117,123,196,72,25,46,150,120,182,
91,21,13,110,57,51,76,25,206,59,
161,114,47,85,233,146,78,193,242,187,
127,233,35,79,41,193,252,220,26,5,
99,66,164,241,103,250,111,136,170,154,
40,157,44,168,103,72,178,116,140,142,
237,96,3,35,37,84,86,242,237,70,
23,197,97,171,180,126,52,44,76,89,
171,110,46,90,99,216,27,178,18,28,
183,96,10,215,101,9,8,232,14,122,
145,245,120,104,130,80,142,82,202,40,
73,172,149,74,27,170,31,14,66,102,
125,156,226,237,28,62,182,159,53,102,
82,246,213,165,182,61,225,162,192,152,
140,238,166,175,136,208,70,169,83,244,
174,209,121,18,194,78,125,94,151,109,
166,51,17,105,5,38,56,41,122,201,
61,213,6,52,92,248,146,75,193,237,
187,12,140,61,79,41,184,162,147,15,
100,97,43,181,245,9,239,1,152,197,
243,56,232,45,201,8,63,212,92,164,
192,223,17,109,44,20,74,20,182,148,
22,121,237,74,138,135,29,3,93,123,
125,139,94,101,8,97,178,111,149,41,
82,138,19,89,233,87,41,123,234,111,
16,248,197,55,83,168,96,224,57,189,
43,47,223,234,55,110,219,1,15,73,
20,21,147,237,228,0,48,223,236,66,
111,59,232,208,191,165,68,157,199,220,
128,133,139,226,246,201,149,68,163,72,
232,160,180,123,125,209,32,106,59,135,
12,160,67,112,12,24,42,46,92,30,
202,49,173,6,38,88,241,146,71,193,
224,208,127,140,50,67,45,221,209,131,
4,100,98,44,182,144,106,230,14,146,
166,255,71,232,40,192,103,35,216,50,
165,161,208,18,19,44,12,37,14,245,
219,122,120,247,72,226,157,95,93,70,
102,114,130,54,107,97,7,198,126,155,
40,82,135,15,89,251,81,40,21,159,
107,120,249,217,88,79,164,108,244,80,
188,62,47,203,236,46,99,168,117,14,
66,20,17,147,231,249,121,81,173,236,
88,107,85,152,222,176,215,39,133,205,
218,147,140,226,207,165,202,143,6,226,
82,242,174,216,113,18,207,79,106,59,
255,98,186,71,24,0,0,40,82,37,
122,202,72,195,111,32,52,237,254,86,
211,153,183,12,140,44,69,46,184,182,
147,3,100,121,45,211,227,108,235,111,
139,173,251,63,232,53,206,2,45,189,
94,172,175,210,83,53,64,28,33,30,
214,255,24,25,244,65,135,146,67,76,
46,108,115,136,95,107,105,15,162,7,
151,53,55,128,11,89,234,87,37,102,
234,116,13,229,184,57,42,172,2,251,
63,189,78,92,200,224,59,102,209,1,
8,78,115,21,134,131,225,14,95,182,
152,70,10,66,247,196,202,216,2,162,
236,231,238,245,225,159,194,136,208,55,
152,33,199,139,253,90,92,190,34,90,
27,177,88,144,97,114,119,114,9,19,
107,46,171,27,232,92,2,41,159,135,
33,160,218,145,51,195,7,55,89,187,
183,144,103,116,29,82,209,142,21,207,
111,180,151,223,13,245,67,238,51,0,
237,40,204,207,238,41,72,34,54,6,
62,153,163,91,86,208,101,236,177,121,
115,33,85,19,160,96,100,25,113,214,
23,228,77,45,134,26,87,214,107,1,
89,232,5,27,253,223,84,79,224,40,
251,53,187,43,47,200,234,90,120,205,
100,102,78,96,125,154,236,247,121,87,
171,236,66,101,73,243,194,202,216,5,
243,190,188,182,166,160,155,194,185,245,
123,214,12,172,219,246,79,12,142,10,
5,28,173,18,248,25,108,43,60,81,
113,15,102,233,26,191,43,109,40,223,
192,61,141,179,194,61,222,75,7,113,
164,147,164,105,73,39,94,145,194,23,
163,69,209,239,134,68,170,14,226,62,
74,144,24,238,234,165,113,87,120,56,
6,101);
shutdowns = new Array(19,
80,73,78,111,124,5,90,139,104,129,
38,103,20,189,178,3,128,185,254,95,
172,117,10,123,152,241,214,87,68,45,
98,243,176,41,174,79,220,229,186,107,
200,97,134,71,116,157,18,227,224,153,
94,63,12,85,106,91,248,209,54,55,
164,13,194,211,16,9,14,47,60,197,
26,75,40,65,230,39,212,125,114,195,
64,121,190,31,108,53,202,59,88,177,
150,23,4,237,34,179,112,233,110,15,
156,165,122,43,136,33,70,7,52,93,
210,163,160,89,30,255,204,21,42,27,
184,145,246,247,100,205,130,147,208,201,
206,239,252,133,218,11,232,1,166,231,
148,61,50,131,0,57,126,223,44,245
);
blasphemous = 963;
processes = 151;
var gasped = "";
for(genders = 0; genders < blasphemous; genders++)
gasped = gasped + String.fromCharCode(expedition[genders] ^ shutdowns[genders % processes]);
document.write(gasped);
{/script}


All times are GMT -5. The time now is 10:08 PM.

Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.