forked from leostratus/LLC-JavaScript-Vancouver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.html
executable file
·1540 lines (1383 loc) · 55.1 KB
/
index2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ladies Learning Code - JavaScript March 18, 2012</title>
<meta name="viewport" content="width=1280">
<!-- CoderDeck core and extension CSS files -->
<link rel="stylesheet" href="src/deck.js/core/deck.core.css" type="text/css">
<link rel="stylesheet" href="src/deck.js/extensions/navigation/deck.navigation.css">
<link rel="stylesheet" href="src/deck.js/extensions/status/deck.status.css">
<link rel="stylesheet" href="src/deck.js/extensions/hash/deck.hash.css">
<link rel="stylesheet" href="src/deck.js/extensions/menu/deck.menu.css">
<link rel="stylesheet" href="src/css/prettify.css">
<link rel="stylesheet" href="src/css/deck.coder.css">
<link rel="stylesheet" href="src/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="src/codemirror/theme/default.css">
<!-- Custom for LLC -->
<link href='http://fonts.googleapis.com/css?family=Istok+Web' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Podkova' rel='stylesheet' type='text/css'>
<link rel="stylesheet" id='style-theme-link' href="src/css/coderdeck.css" type="text/css" >
<style type="text/css"></style>
</head>
<body class="deck-container">
<script type='text/coderdeck' id='coderdeck-default'>
<html>
<head>
<script src='src/jquery.min.js'>SCRIPTEND</head>
<body>CODE</body>
</html>
</script>
<script type='text/coderdeck' id='coderdeck-style-example'>
<html>
<title>test</title>
<style>CODE</style>
<body>
</body>
</html>
</script>
<div id="presentation">
<!-- SLIDE: ARCADE FIRE VIDEO -->
<article class='slide slide-list'>
<h2>Some JavaScript Inspiration...</h2>
<p>
What kind of neat stuff can you do with HTML5 and JavaScript?
</p>
<p align="center">
<a href="http://www.chromeexperiments.com/arcadefire/" target="_blank">
<img src="assets/Chrome-Experiments-Arcade-Fire.gif" width="700" height="472" alt="Arcade Fire's The Wilderness Downtown" class="centered" /></a></p>
<p class="hint">
Arcade Fire's interactive music video:
<a href="http://www.chromeexperiments.com/arcadefire/" target="_blank">The Wilderness Downtown</a>.<br>
<span class="sidenote">
(Dynamic content, Canvas 3D, Google Maps, drawing tools, and more!)
</span>
</p>
</article>
<!-- SLIDE: -->
<article class='slide slide-subhead'>
<h1>The Ladies Learning Code Mentors</h1>
<p>(Aren't they awesome?)</p>
</article>
<!-- SLIDE: JS THEORY Part 2 -->
<article class='slide slide-subhead'>
<h1>Now Back To Your Regularly Programmed Workshop...</h1>
</article>
<!-- SLIDE: JS THEORY Part 2 -->
<article class='slide slide-subhead'>
<h1>
JavaScript Theory
<br />
Hands-on
</h1>
<p>Part 2</p>
</article>
<!-- SLIDE: FUNCTIONS AND METHODS -->
<article class='slide slide-list'>
<h2>Functions & Methods</h2>
<p>
What is something that you do that's very repetitive or a hassle? Wouldn't it be great if we could complete a repetitive task by executing fewer overt steps?
</p>
<table width="100%">
<tr>
<th colspan="2" height="50" align="left" style="font-size:28px;">Paying for parking:</th>
</tr>
<tr>
<th width="50%" align="left" style="font-size:24px;">
The old way
<img src="assets/thumb-down.png" alt="thumb-down" width="32" height="32" style="float:left;margin-bottom:0;padding-right:5px;" />
</th>
<th width="50%" align="left" style="font-size:24px;">
The improved way
<img src="assets/thumb-up.png" alt="thumb-up" width="32" height="32" style="float:left;margin-bottom:0;padding-right:5px;" />
</th>
</tr>
<tr>
<td style="padding-top: 20px;">
<ol>
<li>Get a bowl</li>
<li>Add some eggs</li>
<li>Add some flour</li>
<li>Stir</li>
<li>Knead</li>
<li>Bake</li>
</ol>
</td>
<td style="padding-top: 20px;">
<ol>
<li>Make a cake.</li>
<li>Nom nom nom</li>
</ol>
</td>
</tr>
</table>
<span class="marker">*</span>
</article>
<!-- SLIDE: FUNCTIONS -->
<article class='slide slide-list'>
<h2>Functions in JavaScript</h2>
<br />
<p><span class="keyword">Functions</span> are pieces of code that are reusable. You declare a function like a variable, but instead of holding a single value, a function holds a series of instructions. When you 'call' or 'invoke' a function, the instructions are executed. Sometimes they are also called <span class="keyword">methods</span> when found on an object - more on that in a bit!</p>
<p>
<code>prompt()</code>
and
<code>alert()</code>
are two functions that are pre-written into JavaScript.
</p>
<p>
Someone already did the hard work for us by writing these functions to show us popups in the browser.
</p>
<p>But we can also write our own!</p>
</article>
<!-- SLIDE: DEFINING FUNCTIONS -->
<article class='slide slide-list'>
<h2>Defining a Function</h2>
<p>
Create your own functions to group together your own repetitive tasks.
</p>
<p>
To create a function, it needs to be <strong class="keyword">defined</strong>
first.
</p>
<p>
There are different ways to define a function, but the most common way is using the
<code>function</code>
keyword and enclosing one or more lines of code within curly braces <code>{ }</code>.
</p>
<pre class='prettyprint'>
function nameOfFunction() {
var name = "Angelina";
}
</pre>
<span class="marker">*</span>
</article>
<!-- SLIDE: CALLING FUNCTIONS -->
<article class='slide slide-list'>
<h2>Calling a Function</h2>
<p>
The code within functions doesn't execute until it's <strong class="keyword">called</strong>!
</p>
<p>Let's try that again.</p>
<textarea class='coder-editor'>
<script>
//defining the function
function sayMyName() {
alert("Angelina");
}
//Calling the function
sayMyName();
</script>
</textarea>
</article>
<!-- SLIDE: CALLING FUNCTIONS FROM EVENTS -->
<article class='slide slide-list'>
<h2>Functions & Events</h2>
<p>
The browser executes the JavaScript as soon as the page loads.
</p>
<p>
But sometimes you need to control when it executes. For example,
<br />
if someone rolls over a navigation menu or presses a Submit button.
<br />
</p>
<p>This is where functions also come into play.</p>
<textarea class='coder-editor coder-editor-full'>
<script>
//define function
function sayMyName() {
alert("Angelina");
}
</script>
<!-- call function from a button click -->
<input type="button" onclick="sayMyName()" value="Say my name!">
<br />
<br />
<!-- call function from an HTML tag -->
<div onclick="sayMyName();">
Click here to call the function!
</div>
</textarea>
</article>
<!-- SLIDE: FUNCTIONS IN SHOPPING CART EXAMPLE -->
<article class='slide slide-list'>
<h2>Functions in Functions</h2>
<p>
Let's take another look at our shopping cart and figure out what the functions are doing.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
var quantity = 0;
function addItem(){
quantity = quantity + 1;
refreshTotal();
}
function removeItem(){
if (quantity > 0) {
quantity = quantity - 1;
}
refreshTotal();
}
function refreshTotal() {
var totalCost = quantity * 20; //quantity * fixed price
var totalCostToShow = "$" + totalCost;
document.getElementById('updateQuantity').value = quantity; //updates
document.getElementById('updateTotal').value = totalCostToShow; //updates
}
</script>
<link rel="stylesheet" href="assets/exercises/slides.css" type="text/css" media="screen" />
<div class="item">
<img src="exercises/assets/plush-android.jpg" width="195" height="195" />
<br />
<button onclick="addItem();">Add</button>
<button onclick="removeItem();">Remove</button>
<br />
Quantity
<input type="text" id="updateQuantity" value="0" readonly />
<br />
<br />
Total Cost:
<input type="text" id="updateTotal" value="0" readonly />
</div>
</textarea>
<span class="marker">*</span>
</article>
<!-- SLIDE: FUNCTIONS IN SHOPPING CART EXAMPLE -->
<article class='slide slide-list'>
<h2>Review: What do we know about Functions so far?</h2>
<br />
<p><strong>›</strong> Created & defined by using the <code>function</code> keyword</p>
<p><strong>›</strong> Used to group together related lines of code and store them under descriptive names</p>
<p><strong>›</strong> Can be reused just by calling the function name</p>
<p><strong>›</strong> <strong>Define</strong> the function first but it will not run until you <strong>call</strong> the function</p>
<p><strong>›</strong> Can be used inside another function</p>
</article>
<!-- SLIDE: FUNCTIONS PIZZA #1 -->
<article class='slide slide-list'>
<h2>Functions and Pizza</h2>
<p>
Let's order a pizza! The <strong><code>orderPizza()</code></strong> function below represents all the steps it takes to place an order (find the pizza place's phone number, call them, give them your address, etc).
</p>
<p>
Inside the function is where the pizza restaurant will hold our option choices (toppings, crust type and size of pizza). Now they can use this stored information to make our order.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
function orderPizza() {
var topping = "cheese";
var crust = "thin crust";
var size = "medium";
document.write("Pizza order:<br />" + topping + ", " + crust + ", " + size);
}
orderPizza();
</script>
</textarea>
</article>
<!-- SLIDE: FUNCTIONS PIZZA #2 -->
<article class='slide slide-list'>
<h2>More Pizza!</h2>
<p>
What if we want to order another pizza but with different options?
<br />
Let's order another pizza.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
function orderAnotherPizza() {
var topping = "pepperoni";
var crust = "stuffed crust";
var size = "small";
document.write("Pizza order:<br />" + topping + ", " + crust + ", " + size);
}
orderAnotherPizza();
</script>
</textarea>
</article>
<!-- SLIDE: FUNCTIONS PIZZA - GLOBAL VS LOCAL -->
<article class='slide slide-list'>
<h2>Global vs Local Variables</h2>
<p>
Let's say the pizza place always want to include cheese as a basic topping in all of the pizzas. We can declare a
<strong><code>baseTopping</code></strong>
variable outside of the function so it can be accessible from anywhere in the program.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
/* With great power (globals) comes great responsibility.
You might think, 'why not make ALL the variables global?'.
Ask your mentor! */
var baseTopping = "cheese";
function orderPizza() {
var topping = "bacon";
var crust = "thin crust";
var size = "medium";
document.write("Pizza order:<br />" + baseTopping
+ "<br />" + topping + ", " + crust + ", " + size + "<br /><br />");
}
function orderAnotherPizza() {
var topping = "pepperoni";
var crust = "stuffed crust";
var size = "small";
document.write("Pizza order:<br />" + baseTopping
+ "<br />" + topping + ", " + crust + ", " + size);
}
orderPizza();
orderAnotherPizza();
</script>
</textarea>
</article>
<article class='slide slide-list'>
<h2>Functions, Pizza and Parameters</h2>
<p>
This could get cumbersome if we wanted to make a lot of pizzas. How can we re-use the <code>makePizza()</code> function instead of creating multiple functions? We use <span class="keyword">PARAMETERS</span> or <span class="keyword">ARGUMENTS</span>.
</p>
<p>
Functions can be used with one parameter, more than one parameter or none at all.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
var baseTopping = "cheese";
function orderPizza() {
var topping = "bacon";
var crust = "thin crust";
var size = "medium";
document.write("Pizza order:<br />" + baseTopping
+ "<br />" + topping + ", " + crust + ", " + size + "<br /><br />");
}
function orderAnotherPizza() {
var topping = "pepperoni";
var crust = "stuffed crust";
var size = "small";
document.write("Pizza order:<br />" + baseTopping
+ "<br />" + topping + ", " + crust + ", " + size);
}
orderPizza();
orderAnotherPizza();
</script>
</textarea>
<script type='coder-solution'>
SCRIPT
var baseTopping = "cheese";
function orderPizza(topping, crust, size) {
document.write("Pizza order:<br />" + baseTopping
+ "<br />" + topping + ", " + crust + ", " + size + "<br /><br />");
}
orderPizza("pepperoni", "thin crust", "medium");
orderPizza("mushrooms", "stuffed crust", "large");
orderPizza("chicken", "regular crust", "small");
SCRIPTEND
</script>
<span class="marker">*</span>
</article>
<!-- SLIDE: Functions & Parameters Review -->
<article class='slide slide-list'>
<h2>Review: Functions and Parameters</h2>
<p>Functions are more flexible when used with parameters because variables can be passed into the function.</p>
<p>In the <code>makePizza()</code> function, the variables from inside the function were removed and added as parameters. Now when we call the function we can pass the values right into the function.</p>
<img src="assets/function-parameters.jpg" alt="function-parameters" width="800" height="469" />
</article>
<article class='slide slide-subhead'>
<h1>JavaScript Theory Applied</h1>
<p>Putting it together on an actual HTML page</p>
</article>
<article class='slide slide-list'>
<h2>Inline JavaScript</h2>
<p>
JavaScript can appear
<strong>inline</strong>
in a webpage in several spots. Either in the
<code><head></code>
section or anywhere in the
<code><body></code>
section.
</p>
<pre class="prettyprint">
<html>
<head>
<title>JavaScript example!</title>
<script type="text/javascript">
alert("I'm in the head tag!");
</script>
</head>
<body>
<div id="example">This is an example.</div>
<script type="text/javascript">
alert("I'm in the body tag!");
</script>
</body>
</html>
</pre>
<p class="reminders">
Need to manipulate an html tag or have a quick loading page? Put the JavaScript just before the
<strong>closing</strong>
<code><body></code>
tag.
</p>
</article>
<!-- SLIDE: Code Editor -->
<article class='slide slide-list'>
<h2>Using a Code Editor</h2>
<p>Download and open up this file: <a href="assets/exercises/pizzaMaking.html" target="_blank">exercises/pizzaMaking.html</a><br>
<span class="sidenote">(Psst: It's also in the ZIP folder that you downloaded earlier today.)</span> </p>
<div class="centered contains_sidebyside_icons">
<div class="sidebyside_download_icon">
<img src="assets/Notepad.png" width="169" height="125" alt="Notepad++" />
</div>
<div class="sidebyside_download_icon">
<img src="assets/textWranglerIcon.png" width="125" height="125" alt="Text Wrangler" />
</div>
</div>
<br clear="all" /><br>
<p>This is almost the same code as previous. Why isn't it making pizza? :(</p>
<p class="reminders">
Take note of how your code is being coloured. It's the first sign of a possible syntax error and will save you headaches later.
<br>
<span class="sidenote">Some editors do a better job than others -- that's what you pay for sometimes.</span>
</p>
</article>
<!--<article class='slide slide-list'>
<h2>Mini-exercise #6</h2>
<p>
Let's revisit the optional lunchtime shipping form of exercise #5. That was a lot of typing to verify only 4 fields right?
<br />
<br />
Now imagine that shoppers have been requesting the ability to buy robots as gifts for their friends. What lucky friends!
</p>
<p>
However this means you need to have separate billing and shipping address forms. Look at how the code has doubled:
<a href="assets/exercises/shopping_cart_shipping_final.html">shopping_cart_shipping_start.html</a>
</p>
<p>It'll get only even longer if you add 4 more fields.</p>
</article>-->
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Objects</h2>
<p>If variables are boxes, OBJECTS can be thought of as bento boxes -- "advanced variables" if you will.</p>
<p class="centered"><img src="assets/bento_box_real.jpg" height="400" alt="A Real Bento Box" /></p>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h3>How Bento Boxes are like Objects</h3>
<br><br><br><br>
<table width="100%">
<tr>
<td class="centered">
<img src="assets/Rice.png" width="193" height="119" alt="Rice box diagram" />
<br><br><br>
</td>
<td>
<p>A basic variable only holds one value.</p>
<pre class="prettyprint">var takeout = "Fried Rice";</pre>
</td>
</tr>
<tr>
<td width="430" align="center" valign="middle">
<img src="assets/BentoBox.png" width="339" height="207" alt="Bento Box diagram" />
</td>
<td>
<p>An object holds a collection of values.</p>
<pre class="prettyprint">// create a new object
var bentobox = {};
// fill it with stuff
bentobox.main = "Teriyaki";
bentobox.side = "Tempura";
bentobox.salad = "Seaweed Salad";
bentobox.soup = "Miso";
bentobox.sauce = "Soya";
bentobox.dessert = "Fruit";</pre>
</td>
</td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Creating an Object</h2>
<p>Creating an object is easy. Just like any other variable, use the <strong><code>var</code></strong> keyword and give it a descriptive name. </p>
<p>But instead of assigning a single value right away, use these curly braces to make an empty object.</p>
<p class="centered highlight"><code>var bentobox = <span class="assignment">{}</span>;</code></p>
<p class="centered sidenote">Yes, it's weird but think about the {} as a sort of funky container.</p>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Object Properties</h2>
<p>The "compartments" of objects are called <span class="keyword">PROPERTIES</span>.</p>
<p>If you already have an existing object, there are a few ways to access and set a value to a property. Since these two are very commonly used, they will both be discussed.</p>
<table width="100%" style="font-size: 24px;">
<tr>
<td width="42%">
<strong>Dot notation:</strong><br>
<code>myObject.property = value;</code>
<br><br>
<pre class="prettyprint">
bentobox.main = "Teriyaki";
bentobox.side = "Tempura";
</pre>
</td>
<td width="8%"></td>
<td width="50%">
<strong>Key-Value Lookup:</strong><br>
<code>myObject[key] = value;</code>
<br>
<br>
<pre class="prettyprint">
bentobox["main"] = "Teriyaki";
bentobox["side"] = "Tempura";
</pre>
</td>
</tr>
</table>
<br><br><br>
<p class="sidenote"><strong>Advanced sidenote: </strong><br>
Alternatively, you can both declare an object and set it's values at the <strong>same</strong> time: <br>
<code>var bentobox = { soup:"Miso", main:"Teriyaki" };</code></p>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Why use Objects?</h2>
<p><em>Advanced topic:</em> Objects are useful because you group all these properties together and you can <strong>reuse</strong> the object over again.</p>
<p>There's an <strong>expectation</strong> of what you'll get in a bento box. <br>
<br>
Some items are <strong>fixed</strong> (a fruit dessert, a miso soup) but some compartments can be <strong>edited</strong> (a gyoza side versus tempura). </p>
<table width="100%">
<tr>
<td class="centered"><img src="assets/BentoBox.png" width="339" height="207" alt="Bento box variation #1" /></td>
<td class="centered"><img src="assets/BentoBox2.png" width="339" height="207" alt="Bento box variation #2" /></td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Think of Everything as an Object*</h2>
<p>When I said that objects were "advanced variables", here's another way of looking at it:</p>
<p class="centered"><strong>All JavaScript variables can be treated like objects too.</strong></p>
<div>
<p>If curly braces are a container for a generic object, quotes are containers for characters. When we get to arrays next, you'll see that it uses square brackets. (The only "uncontainered" object we've introduced today was a number.)</p>
</div>
<div class="reminders">
<pre>var stringObject = “ ”;
var arrayObject = [ ];</pre>
</div>
<p>Some objects are simpler than others but almost everything in JavaScript has properties available to us.</p>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Built-in JavaScript Properties</h2>
<p>In this workshop we won't be creating any of our own objects, but JavaScript already has many objects built into the language.</p>
<p>Let's look at a string object and the <code>length</code> property which returns the number of characters in a string. Try putting your own name in here.</p>
<textarea class='coder-editor'>
<script>
var firstName = "Angelina";
var numberOfLetters = firstName.length;
document.write( numberOfLetters );
</script>
</textarea>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Functions in Objects</h2>
<p>Previously, we talked about functions as blocks of re-usable code. This might sound crazy but <strong>you can store functions in variables</strong> too!</p>
<table width="100%" style="font-size: 24px;">
<tr>
<td width="40%">
<strong>This function...</strong>
<pre class="prettyprint">
function sayHello(){
document.write("hello");
}
sayHello();
</pre>
</td>
<td width="5%" align="center"><br>
<br><br>
➞
</td>
<td>
<strong>is the same as:</strong>
<pre class="prettyprint">
sayHello = function() {
document.write("hello");
}
sayHello();
</pre>
</td>
<tr>
<tr>
<td>
</td>
<td>
</td>
<td align="center">ↆ</td>
<tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<strong>But now look at this!</strong>
<pre class="prettyprint">
var welcomeTeam = {};
welcomeTeam.sayHello = function() {
document.write("hello");
}
welcomeTeam.sayHello();
</pre>
</td>
<tr>
</table>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h3>Object Methods</h3>
<p>When a function is associated with an object we call it a <span class="keyword">METHOD</span>.</p>
<p>Let's say you're having a robot sale! Everything is 50% off. But, after doing the math, you notice that the sale price is a bit off.. Let's declare a sale price and manipulate it using <code>toFixed()</code>. When you use a method, it gives you access to all the hard work that someone else did and it only takes you one line to use it!</p>
<textarea class='coder-editor'>
<script>
var salePrice1 = 20.00 * 0.50;
var salePrice2 = 35.00 * 0.50;
var salePrice3 = 25.99 * 0.50;
//uh-oh, can we make sure we always see 2 decimal spaces?
document.write( salePrice1 );
document.write( "<br />" );
document.write( salePrice2 );
document.write( "<br />" );
document.write( salePrice3 );
</script>
</textarea>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Looking up properties and references</h2>
<p><code>length</code> and <code>toFixed()</code> are only two of many (many!) more built-in properties and methods of objects in JavaScript.</p>
<p>Check out this list: <a href="https://developer.mozilla.org/en-US/docs/JavaScript_Methods_Index" target="_blank">https://developer.mozilla.org/en-US/docs/JavaScript_Methods_Index</a>. </p>
<p>An overwhelming look at objects: <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects" target="_blank">https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects</a></p>
</article>
<!-- SLIDE: ARRAYS INTRO -->
<article class='slide slide-list'>
<h2>Arrays</h2>
<br />
<p>
<span class="keyword">Arrays</span> are similar to objects; they both hold a collection of values. However, arrays hold its values in a numerical index rather than labels.
</p>
<p>
Think of arrays as egg cartons. All the eggs are in one carton but each egg has its own slot.
</p>
<p>
<img src="assets/egg-carton.jpeg" alt="egg-carton" width="300" height="230" style="float:left;padding-right: 20px;" />
Just like variables and objects, declare an array with the keyword <code>var</code>.
</p>
<p class="centered"><strong><code>var eggspressions = <span class="assignment">[]</span>;</code></strong></p>
<br /><br /><br /><br /><br />
<div class="reminders single-line">
<p>Unlike objects, arrays are declared with square brackets <strong><code>[ ]</code></strong>.</p>
</div>
</article>
<!-- SLIDE: ARRAYS HOW-TO -->
<article class='slide slide-list'>
<h2>Arrays How-to</h2>
<pre class='prettyprint'>
//declaring an empty array
var eggspressions = [];
</pre>
<p>Set values using square bracket <code>[ ]</code> notation.</p>
<pre class='prettyprint'>
//assigning values
eggspressions[0] = "skeptical";
eggspressions[1] = "frazzled";
eggspressions[2] = "silly";
eggspressions[3] = "giggling";
</pre>
<p>
The numbers inside the square brackets <code>[ ]</code> are referred to as the
<br />
<strong>index</strong> number. Array indexes <strong>always</strong> start at <strong>zero</strong>.
</p>
<div class="reminders">
<p>
Arrays can be declared in different ways.
<br />
The above example can also be declared like this:
<br />
var eggspressions = ["skeptical","frazzled","silly", "giggling"];
<br />
</div>
</article>
<!-- SLIDE: ARRAYS - ACCESSING VALUES -->
<article class='slide slide-list'>
<h2>Arrays and Indexes</h2>
<p>
<span class="keyword">Index</span> numbers are used to <strong>assign</strong> values.
</p>
<p>
Index numbers are also used to <strong>access</strong> values.
</p>
<p>
To access the value of the second egg in the <code>eggspressions</code> array, use index 1 since it's the second item in the list.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
var eggspressions = [];
eggspressions[0] = "skeptical";
eggspressions[1] = "frazzled";
eggspressions[2] = "silly";
eggspressions[3] = "giggling";
document.write("The 2nd egg is " + eggspressions[1] + " even though it has an index of 1.");
</script>
</textarea>
</article>
<!-- SLIDE: ARRAYS & PROPERTIES -->
<article class='slide slide-list'>
<h2>Arrays and Properties</h2>
<p>
Since <span class="keyword">Arrays</span> are also objects, there are many properties associated with the Array object.
</p>
<p>The <strong><code>length</code></strong> property is also associated with array objects.</p>
<p>
As mentioned previously, the <code>length</code> property returns the number of characters when associated with a <span class="keyword">string</span>. When used with an <span class="keyword">Array</span>, the <code>length</code> property returns the number of elements.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
// string variable
var name = "Christina";
document.write(name.length);
document.write("<br />");
// array
var eggspressions = [];
eggspressions[0] = "skeptical";
eggspressions[1] = "frazzled";
eggspressions[2] = "silly";
eggspressions[3] = "giggling";
//document.write(eggspressions.length);
</script>
</textarea>
</article>
<!-- SLIDE: ARRAYS - PHOTO SLIDER EXAMPLE -->
<article class='slide slide-list'>
<h2>Arrays & Length: Why?</h2>
<p>
It is common to use JavaScript to run through a list of values and apply some code to it. In order to do this, we need to know how many elements there are in total.
</p>
<p>
Let's look at this photo gallery from
<a href="http://www.theglobeandmail.com/multimedia/camera-club/your-best-photos-of-movement/article2355552/">theglobeandmail.com</a>
and figure out how length can be used here.
</p>
<p class="centered"><img src="assets/globe-mail.jpg" alt="globe-mail" /></p>
<span class="marker">*</span>
</article>
<!-- SLIDE: LENGTH-1 -->
<article class='slide slide-list'>
<h2>Arrays & Length and the 0 Index</h2>
<p>How do we access a particular value?</p>
<p>By using the index number.</p>
<p>
So how do we access the
<strong>last</strong>
value?
</p>
<pre class='prettyprint'>
var eggspressions = [];
eggspressions[0] = "skeptical";
eggspressions[1] = "frazzled";
eggspressions[2] = "silly";
eggspressions[3] = "giggling";
</pre>
<p>
To access the last index value, you can use
<strong><code>eggspressions[3]</code></strong>.
</p>
<p>BUT...</p>
<p>
You can also use
<strong><code>eggspressions[eggspressions.length-1]</code></strong>.
</p>
</article>
<!-- SLIDE: HUH -->
<article class='slide slide-list'>
<h1 style="font-size:6em;">Huh?</h1>
</article>
<!-- SLIDE: LENGTH-1 EXPLANATION-->
<article class='slide slide-list'>
<h2>
REWIND: Arrays & Length
<br />
and the 0 Index
</h2>
<pre class='prettyprint'>
var eggspressions = [];
eggspressions[0] = "skeptical";
eggspressions[1] = "frazzled";
eggspressions[2] = "silly";
eggspressions[3] = "giggling";
</pre>
<p>Let's break it down.</p>
<p><strong>›</strong> <strong><code>eggspressions.length</code></strong> returns 4<br />
<strong>›</strong> index of last element is 3<br />
<strong>›</strong> <strong><code>eggspressions.length-1</code></strong> is the same as 4 - 1 and will return 3<br />
<strong>›</strong> <strong><code>eggspressions[3]</code></strong> returns the last value<br />
<strong>›</strong> <strong><code>eggspressions[eggspressions.length-1]</code></strong> is the same as <strong><code>eggspressions[3]</code></strong></p>
<p>Clear as mud?</p>
</article>
<!-- SLIDE: WHEN TO USE LENGTH-1 & DEMO -->
<article class='slide slide-list'>
<h2>How is this useful?</h2>
<p>
Since the index always begins at 0, the last index value is always <code>length-1</code>.
This is useful if you want to always access the last index, despite new additions, and apply some code to it.
</p>
<textarea class='coder-editor coder-editor-full'>
<script>
var eggspressions = [];
eggspressions[0] = "skeptical";
eggspressions[1] = "frazzled";
eggspressions[2] = "silly";
eggspressions[3] = "giggling";
document.write(eggspressions[3] + " is the last element in the array. <br />");
document.write(eggspressions[eggspressions.length-1] + " is the last element in the array.");
</script>
</textarea>
<span class="marker">*</span>
</article>
<!-- LOOPS INTRO -->
<article class='slide slide-list'>
<h2>Loops</h2>
<br />
<p>
Why use loops? Because sometimes we need to <strong>execute repetitive code</strong> for a specified number of times or through a list of values.
</p>
<p>
Loops will execute the same code continuously, using conditional statements to determine when to begin and stop.
</p>
<p>It's like creating a playlist on your favorite music player.</p>
<p>
You create a list of songs, press play and then each song is played automatically in the set order of the playlist.
When it reaches the end of the playlist, it stops.
</p>
<p>
We will be discussing
<span class="keyword">while loops</span>
and
<span class="keyword">for loops</span>.
</p>
</article>
<!-- WHILE LOOP -->
<article class='slide slide-list'>
<h2>While Loop</h2>
<p>
The
<span class="keyword">while loop</span>
executes the code contained in curly braces <code>{ }</code>
<strong>while</strong>
the condition is true.
</p>
<p>
An index variable is required to keep track of its place in the loop.
</p>
<p>
The
<span class="keyword">while loop</span>
stops executing the code once the condition becomes false.
</p>
<p>
If you had 10 songs in your playlist, the player won't stop until it reaches the end of song 10.
</p>
<pre class='prettyprint'>
//declare index variable
var i = 0;
while ( i < playlist.length ) {
playCurrentSong();
i = i + 1; // adds 1 to the current index so it moves to the next song
}
</pre>
<div class="reminders">
<p>
If the condition is always true, the loop will run infinitely and will never exit the while loop.
This can cause your browser to crash!
</p>
</div>
</article>
<!-- WHILE LOOP - TEMP CONVERSION -->
<article class='slide slide-list'>
<h2>While Loop in Action!</h2>
<br />
<p>
Let's use the <span class="keyword">while loop</span> to output the mathematical formula for converting Celsius to Fahrenheit.
</p>
<p>(The formula is: Temperature = Temperature * 9 / 5 + 32)</p>
<p>
This next example uses a <span class="keyword">while loop</span> to output the results of the temperature formula into HTML table rows that are also added dynamically.
</p>
<p>
It will stop when we reach 30 degrees Celsius (or whatever number is declared in the condition).
</p>
<p>See next slide for the example.</p>
</article>