Upload
teresa-pelkie
View
756
Download
0
Embed Size (px)
DESCRIPTION
Instructor Teresa Pelkie at Palomar College - Variables, Calculations, "if" statement
Citation preview
1
JavaScript – Class 2
• prompt()• operators• Calculations• if statement
prompt("Please enter your name", "");
2
<head>
<script language="JavaScript">
var myPrompt;
myPrompt = prompt("Please enter your name", "");
</script>
</head>
<body>
<script language="JavaScript">
document.write(myPrompt);
</script>
</body>3
Mathematical operators: + - / * % •
Precedence - * and / are performed first
unless you use parenthesis
Assignment operator: = also assigns a value
to a variable
Increment and decrement operators: ++ --
4
Comparison operators: < <= > >= == !=
Logical operators: && || !
Commonly used with an “if” statement
5
<script language="JavaScript">
alert(1 + 1);
</script>
<script language="JavaScript">
var firstNumber = 40;
var secondNumber = 20;
alert(firstNumber + secondNumber);
</script> 6
NaN – Not a Number
a special value in JavaScript that is returned if
you do a calculation with a non-number character
IsNaN() – function
tests if a variable is a number or not
– returns a Boolean data type- returns true if it is a string (because a string is not a
number) - returns false if it is a number
7
<script language="JavaScript">
alert(isNaN("teresa"));
</script>
8
See page 6 in handout
if statement
<script language=“JavaScript”>
var variable1 = 123;
var variable2 = 123;
if (variable1 == variable2)
{
alert ("condition true");
}
</script>9
no semicolon
== means:is the same oris equal or is true
the condition to be evaluated is inside the parenthesis
code to be executed if the condition is true
see page 7 handout
Using comparison and logical operators
<script language=“JavaScript”>
var variable1 = 2;
var variable2 = 4;
var variable3 = 6;
if ((variable1 < variable2) || (variable2 < variable3))
{
alert ("condition true");
}
</script>10See page 8 and 9 in handout
if else statement
11
see page 10 handout
Gives you the ability to execute code when the condition evaluates to true and also when it evaluates to false
if (condition) { code here will execute if condition is true’ } else { code here will execute if condition is false; }
if else statement
12
see page 10 handout
<script language=“JavaScript”>
var variable1 = 123; var variable2 = 124;
if (variable1 == variable2) { alert ("true"); } else { alert("false"); }
</script>
if else if statement
13
see page 11 handout
var variable1 = 123;var variable2 = 124;
if (variable1 == variable2) { alert("equal"); }
else if(variable1 < variable2) { alert("less than"); }
else { alert("none of the above"); }
“else if “block can be repeated as needed
14
JavaScript – Class 2
Assignment 2