Documente Academic
Documente Profesional
Documente Cultură
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 (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 !
E.g.
E.g.
If (!(blue == green))
{ document.writeln(The colours are not the same); }