These are the programs I am going to cover today. 1
2
Javascript is embedded in HTML. The document.write() will write the literal Hello World! to the web page document. Then the alert() puts out a pop up that says Hello World (shown on previous slide). 3
4
Now I am going to add a h1 header to the <html> just before the script Look at the next slide to see the results. 5
6
Note it should be charset I have an extra a in this and most of my examples thanks to copy and paste. I have fixed them online now. Now I am going to look at a web page set up with the usual HTML and including an h1 in HTML and h1 in Javascript. 7
8
the 0 is in the box when the prompt is displayed 9
10
11
When writing your HTML page, you follow the same rules that you did before you ae simply inserting Javascript. 12
13
14
15
16
Note that I can tell there is an error because the Javascript is showing up rather than being implemented. This had to be debugged. 17
Somehow I really messed up with copy and paste as you can see. 18
Finally I got the code straightened out! 19
20
I entered 0s quickly to see if it was cleaned up probably should have entered real numbers to see the multiply 21
22
I entered 5 and then 7 on the second prompt. Note that a + can mean concatenate and add. I do not specify datatype in Javascript so it has to figure out which to do. Calculating ans used concatenating and I see 57. If I do a convert on the number then Javascript determines I want to add. I see otherans after the add showing 12. 23
Someone asked if this would work it DID NOT. 24
But it did give me a way to show you how to find errors. In Firefox, go to Tools and then Web Developer and Error Console. 25
It pointed out the problem. 26
Ways to impact the data to cause the add to happen. parsefloat parseint + Number 27
28
I entered 5.55 and 4.44. 29
First I got prompted for two numbers and then I had to choose multiplication or division. 30
Here I am making a decision. My condition is to check and see if whattodo == "*" Note that when comparing for equality we use ==. What to do if whattodo does equal * is enclosed in the first set of curly braces. Otherwise I will take the else and what to do it it is false is enclosed in another set of curly braces. Note that the = here means assign. I do the calculation and assign it to ans. Note also that anything other than an * executes the else which means if I enter / it will do the else and if I enter Z it will do the else. 31
I could have said var whattodo = ""; 32
33
I entered 5 and then 6 and then / or something other than * and this was the result. 34
35
As shown in the previous slide, I entered + which is not an * and not a / so it is Problem. 36
37
38
Now I want to check for *, /, + or. 39
40
41
42
Can do with separate if statements but then the last one gets complex when you need to check for not any of the acceptable entries and you also have every decision checked every time. Not my favorite approach! 43
44
45
46
47
48
49
If the actions here are the same, you can use a compound otherwise simple if statements. 50
Compound since I do not care which is not true, I want to say not eligible. 51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66