14
1 JavaScript – Class 2 prompt() operators Calculations if statement

CSIS 138 JavaScript Class2

Embed Size (px)

DESCRIPTION

Instructor Teresa Pelkie at Palomar College - Variables, Calculations, "if" statement

Citation preview

Page 1: CSIS 138 JavaScript Class2

1

JavaScript – Class 2

• prompt()• operators• Calculations• if statement

Page 2: CSIS 138 JavaScript Class2

prompt("Please enter your name", "");

2

Page 3: CSIS 138 JavaScript Class2

<head>

<script language="JavaScript">

var myPrompt;

myPrompt = prompt("Please enter your name", "");

</script>

</head>

<body>

<script language="JavaScript">

document.write(myPrompt);

</script>

</body>3

Page 4: CSIS 138 JavaScript Class2

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

Page 5: CSIS 138 JavaScript Class2

Comparison operators: < <= > >= == !=

Logical operators: && || !

Commonly used with an “if” statement

5

Page 6: CSIS 138 JavaScript Class2

<script language="JavaScript">

alert(1 + 1);

</script>

<script language="JavaScript">

var firstNumber = 40;

var secondNumber = 20;

alert(firstNumber + secondNumber);

</script> 6

Page 7: CSIS 138 JavaScript Class2

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

Page 8: CSIS 138 JavaScript Class2

<script language="JavaScript">

alert(isNaN("teresa"));

</script>

8

See page 6 in handout

Page 9: CSIS 138 JavaScript Class2

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

Page 10: CSIS 138 JavaScript Class2

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

Page 11: CSIS 138 JavaScript Class2

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; }

Page 12: CSIS 138 JavaScript Class2

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>

Page 13: CSIS 138 JavaScript Class2

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

Page 14: CSIS 138 JavaScript Class2

14

JavaScript – Class 2

Assignment 2