Sunteți pe pagina 1din 14

Website design

Last Week
JavaScript
Creating HTML using JavaScript Using JavaScript to get user input Manipulating user input and outputting to the web browser

This Week
More advanced concepts of JavaScript and programming languages in general Mathematical equations Logic Conditions/If statements

Formulas
The symbols for adding, subtracting, multiplying and division are as follows

Addition
+

Subtraction Multiplication
*

Division /

Formulas in JavaScript
E.g. to do (5 * 6) / 2 <script language = JavaScript> var firstnumber, secondnumber,thirdnumber, total; firstnumber = 5; secondnumber = 6; thirdnumber = 2; total = (firstnumber * secondnumber) / thirdnumber; document.writeln(The sum is : + total); </script>

Decision making
If statement e.g.

If (firstname == Mark) { document.writeln(His name is Mark); } If (firstname == Lydia) { document.writeln(Her name is Lydia); } If (firstname == Robbie) { document.writeln (His name is Robbie); }

Decision Making
The Else statement

Can be used to find the false case should the original If be incorrect E.g.

If (name == bob) { document.writeln (Name is Bob); } Else { document.writeln (Name is not Bob); }

Decision making
Operators not to be confused with formulas

Operators are like the symbol on the last slide : == There are 6 main operators Equal to : == (two equal symbols to represent equality i.e bob == bob is true) Not Equal to : != Greater than : > Less than : < Greater than or equal to : >= Less than or equal to : <=

Decision making
The outcome of the operators will either be true or false E.g. 5 = 5 is true E.g. 4 = 5 is false So if you use a variable which is set to a value

e.g. name = frank (note :this is an assigning = symbol), then the below operator would be true and you could have

if (name == frank) { document.writeln(His name is Frank); }

Using it to do the hard work


E.g. You could use it to distinguish between someones grade

If (studentGrade >= 90) Document.writeln(A); Else If (studentGrade >= 80 document.writeln(B); Else if (studentGrade >=70); document.writeln(C); Else if (studentGrade >= 60); document.writeln(D); else document.writeln(F);

AND/OR/NOT operators
In JavaScript there are three main logical concepts
AND represented by && OR represented by || (the symbol next to the left hand shift key, pressed twice) NOT represented by an exclamation mark !

Uses of logical operators


AND - For the condition when two events are true

E.g.

If ((name == bob)&&(age >= 20))


{ document.write(My name is : + name + and I am of age + age); }

Uses of Logical operators


OR For the condition when one of two conditions could be true

E.g.

If ((team==Everton)||(team==Liverpool)) { document.writeln(You support a football team from Merseyside); }

Uses of logical operators


NOT For the condition when something is not true

If (!(blue == green))
{ document.writeln(The colours are not the same); }

S-ar putea să vă placă și