-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1734 lines (1327 loc) · 67.8 KB
/
index.html
File metadata and controls
1734 lines (1327 loc) · 67.8 KB
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 lang="en">
<head>
<meta charset="utf-8">
<title>Intro to JavaScript Slides</title>
<meta name="description" content="Intro to JavaScript slides">
<meta name="author" content="Liz Krane">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/simple.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor --><link rel="stylesheet" href="lib/css/light.css">
<!-- dark editor <link rel="stylesheet" href="lib/css/dark.css">-->
<!-- <link rel="stylesheet" href="lib/css/zenburn.css"> -->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="css/print/pdf.css" type="text/css" media="print">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 style="line-height:1.1em; margin-top:0;margin-bottom:60px;">Intro to JavaScript</h1>
<p style="margin-top:100px;">
Slides by <a href="https://learningnerd.com">Liz Krane</a> / <a href="https://twitter.com/LearningNerd">@LearningNerd</a>
</p>
</section>
<section>
<h2>Tell us about yourself!</h2>
<ul>
<li>What's your name?</li>
<li>What awesome things do you hope to make with code?</li>
<li>What's one question you have about JavaScript?</li>
</ul>
</section>
<section>
<h2 style="color:#53555C">Bookmark These Slides:</h2>
<p style="margin-top:90px; font-size:1.8em; font-weight: bold;">
<a href="http://learnteachcode.org/javascript-intro/" style="font-size:0.75em;">LearnTeachCode.org/javascript-intro</a>
</p>
<p style="margin-top:90px;">Follow along on your laptop or phone!</p>
</section>
<section>
<h2>What is JavaScript?</h2>
<p style="margin-top:75px;">A quirky but powerful and ubiquitous programming language -- it's built into every modern web browser!</p>
<p style="margin-top:50px;" class="fragment">Originally a client-side language for making websites more interactive, but now <em>so much more</em>!</p>
</section>
<section>
<a href="http://redmonk.com/sogrady/2017/03/17/language-rankings-1-17/"><img src="img/redmonk-language-rankings-2017.png" class="noborder"></a>
<p style="margin-top:10px;"><a href="http://redmonk.com/sogrady/2017/03/17/language-rankings-1-17/">Popularity of Programming Languages as of January 2017</a>. Stats by RedMonk, based on data from GitHub and StackOverflow</p>
</section>
<section>
<h2 style="color:#53555C;font-size:1.2em;line-height:1em;">A Few of My Favorite JavaScript Libraries</h2>
<p><a href="http://paperjs.org/">PaperJS.org</a></p>
<p><a href="https://nodejs.org/en/">NodeJS.org</a></p>
<p><a href="https://d3js.org/">d3JS.org</a></p>
<p><a href="http://jlord.us/sheetsee.js/">jlord.us/sheetsee.js</a></p>
<p><a href="http://johnny-five.io/">johnny-five.io</a></p>
<p><a href="http://leafletjs.com/">leafletJS.com</a></p>
<p><a href="http://letteringjs.com/">letteringJS.com</a></p>
<p><a href="http://underlinejs.org/">underlineJS.org</a></p>
<p><a href="http://phaser.io/">phaser.io</a></p>
<p><a href="http://www.createjs.com/soundjs">createJS.com/soundjs</a></p>
<p><a href="http://lab.hakim.se/reveal-js/">revealJS.com</a></p>
<p><a href="https://www.smore.com/clippy-js">smore.com/clippy-js</a></p>
</section>
<section>
<h2>Let's build something with JavaScript!</h2>
</section>
<section>
<h3 style="margin-bottom: 30px;margin-top:-50px;">How to open your web browser's JavaScript console:</h3>
<ul>
<li><strong>Chrome:</strong>
<ul><li style="margin-top:20px;">
<strong><em>Windows:</em></strong> Ctrl + Shift + <strong>J</strong>
</li>
<li>
<strong><em>Mac:</em></strong> Cmd + Opt + <strong>J</strong>
</li>
</ul>
</li>
<li><strong>Firefox:</strong>
<ul><li style="margin-top:20px;">
<strong><em>Windows:</em></strong> Ctrl + Shift + <strong>K</strong>
</li>
<li>
<strong><em>Mac:</em></strong> Cmd + Opt + <strong>K</strong>
</li>
</ul></li>
<li><strong>Safari / <em>Mac:</em></strong> Cmd + Opt + <strong>C</strong>
<ul><li style="margin-top:20px;">
<em>Note: first you may need to enable web inspector (<a href="https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/GettingStarted/GettingStarted.html">see instructions here</a>)</em>
</li></ul>
</li>
</ul>
</section>
<!-- <section>
<h3 style="margin-bottom: 10px;">How to open your web browser's JavaScript console</h3>
<ul>
<li><strong>Chrome:</strong>
<ul><li style="margin-top:20px;">
<strong><em>Windows:</em></strong> Ctrl + Shift + <strong>J</strong>
</li>
<li>
<strong><em>Mac:</em></strong> Cmd + Opt + <strong>J</strong>
</li>
</ul>
</li>
<li><strong>Firefox:</strong>
<ul><li style="margin-top:20px;">
<strong><em>Windows:</em></strong> Ctrl + Shift + <strong>K</strong>
</li>
<li>
<strong><em>Mac:</em></strong> Cmd + Opt + <strong>K</strong>
</li>
</ul></li>
<li><strong>Safari / <em>Mac:</em></strong> Cmd + Opt + <strong>C</strong>
<ul><li style="margin-top:20px;">
<em>Note: first you may need to enable web inspector (<a href="https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/GettingStarted/GettingStarted.html">see instructions here</a>)</em>
</li></ul>
</li>
</ul>
</section> -->
<!--
<section>
<div style="float:left;width: 75%">
<pre class="fragment fade-out overlay" data-fragment-index="0"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// Now let's use JavaScript to interact with the HTML!
// First try clicking on the button to the right > > >
// It doesn't do anything yet, but we'll fix that!
</code></pre>
<pre class="fragment current-visible overlay" data-fragment-index="0"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// The HTML for the button is:
// <button id="testbutton">Click me!</button>
// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');
</code></pre>
<pre class="fragment current-visible overlay"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// The HTML for the button is:
// <button id="testbutton">Click me!</button>
// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');
// and change the text inside it like this:
button.textContent = 'testing one two three!';
</code></pre>
<pre class="fragment current-visible overlay"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// The HTML for the button is:
// <button id="testbutton">Click me!</button>
// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');
// and change the text inside it like this:
button.textContent = 'testing one two three!';
// Let's do the same with the box below the button:
var displayBox = document.getElementById('testdisplay');
</code></pre>
<pre class="fragment current-visible overlay"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// The HTML for the button is:
// <button id="testbutton">Click me!</button>
// So we can create a variable for the button like this:
var button = document.getElementById('testbutton');
// and change the text inside it like this:
button.textContent = 'testing one two three!';
// Let's do the same with the box below the button:
var displayBox = document.getElementById('testdisplay');
// and change the text inside the box too:
displayBox.textContent = 'JavaScript is magical!';
</code></pre>
<pre class="fragment current-visible overlay"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// Now let's define a new function that does this:
function sayHello() {
displayBox.textContent = 'Hello, world!';
};
// And let's call the function to check that it works:
sayHello();
</code></pre>
<pre class="fragment current-visible overlay"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// Now let's define a new function that does this:
function sayHello() {
displayBox.textContent = 'Hello, world!';
};
// Finally, let's run this function when the button is clicked!
button.addEventListener('click', sayHello);
// It has 3 parts:
// 1) the HTML element where the event happens (button)
// 2) the type of event ('click')
// REFERENCE:
// https://developer.mozilla.org/en-US/docs/Web/Events
// 3) the function to run (sayHello)
</code></pre>
<pre class="fragment current-visible overlay"><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// The complete code:
var button = document.getElementById('testbutton');
var displayBox = document.getElementById('testdisplay');
function sayHello() {
displayBox.textContent = 'Hello, world!';
};
button.addEventListener('click', sayHello);
// Tinker with it here:
// https://codepen.io/LearnToCodeLA/pen/ZONyjP
</code></pre>
</div>
<div style="float:left;margin-top:70px;width: 25%;">
<button id="testbutton" class="testbutton" style="font-size: 40px; padding: 20px;">Click me!</button>
<p id="testdisplay" style="border: 2px solid black; font-size:30px; padding: 20px; margin-top: 40px;">
Display messages here
</p>
</div>
</section>
-->
<section>
<iframe height='542' scrolling='no' src='https://codepen.io/LearnTeachCode/embed/preview/ZONyjP/?height=542&theme-id=dark&default-tab=result,js&embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'><a href='https://codepen.io/LearnTeachCode/pen/ZONyjP/'>Click here to open this Codepen!</a>
</iframe>
</section>
<section>
<!-- <iframe height='542' scrolling='no' src='https://codepen.io/LearnTeachCode/embed/gWwBbp/?height=542&theme-id=0&default-tab=js,result&embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'><a href='https://codepen.io/LearnTeachCode/pen/gWwBbp/'>Click here to open this Codepen!</a>
</iframe> -->
<iframe height='542' scrolling='no' src='https://codepen.io/LearnTeachCode/embed/preview/rxjbYd/?height=542&theme-id=0&default-tab=result,js&embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'><a href='https://codepen.io/LearnTeachCode/pen/rxjbYd/'>Click here to open this Codepen!</a>
</iframe>
</section>
<!--
<section data-state="movethebox">
<div style="float:left;width:75%">
<pre class=""><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// Press W, A, S, and D on your keyboard to move the box!
var box = document.getElementById('movebox');
window.addEventListener('keydown', moveBox);
function moveBox(event) {
if (event.keyCode == '83') {
var currentTopPosition = parseInt(box.style.top, 10);
var newTopPosition = currentTopPosition + 10;
box.style.top = newTopPosition + 'px';
}
if (event.keyCode == '87') {
var currentTopPosition = parseInt(box.style.top, 10);
var newTopPosition = currentTopPosition - 10;
box.style.top = newTopPosition + 'px';
}
if (event.keyCode == '68') {
var currentLeftPosition = parseInt(box.style.left, 10);
var newLeftPosition = currentLeftPosition + 10;
box.style.left = newLeftPosition + 'px';
}
if (event.keyCode == '65') {
var currentLeftPosition = parseInt(box.style.left, 10);
var newLeftPosition = currentLeftPosition - 10;
box.style.left = newLeftPosition + 'px';
}
}
</code></pre>
</div>
<div style="float:left;margin-top:100px;width:25%;">
<p id="movebox" style="width:200px;height:200px;background:lightblue;position:absolute;top:110px;left:820px;transition:0.1s;"></p>
</div>
</section> -->
<section>
<iframe height='542' scrolling='no' src='https://codepen.io/jeffibacache/embed/bzBsp/?height=542&theme-id=dark&default-tab=result&embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen <a href='https://codepen.io/jeffibacache/pen/bzBsp/'>Playable Canvas Asteroids</a> by Jeff Ibacache (<a href='https://codepen.io/jeffibacache'>@jeffibacache</a>) on <a href='https://codepen.io'>CodePen</a>.
</iframe>
</section>
<section>
<h2>Group Googling Challenge:</h2>
<h3>Make a Box Turn Random Colors!</h3>
</section>
<section>
<div style="float:left;width:75%">
<pre><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
var colorBox = document.getElementById('colorbox');
colorBox.addEventListener('click', randomColor);
function randomColor() {
// Your code here! Go Googling!
// HINT 1: Find out how to generate a random hex color
// HINT 2: Find out how to use JavaScript to change
// the background color of an element
// HINT 3: Don't forget hex colors need a # in front!
}
</code></pre>
</div>
<div style="float:left;margin-top:100px;width:25%;">
<p id="colorbox" style="width:200px;height:200px;background:lightblue;position:absolute;top:110px;left:820px;transition:0.1s;"></p>
</div>
</section>
<section data-state="colorthebox">
<div style="float:left;width:75%;">
<pre><code class="javascript smaller" data-trim contenteditable="" style="height:470px;">
// Did you learn anything interesting? :)
// HERE'S THE SOLUTION:
var colorBoxSolved = document.getElementById('colorboxsolved');
colorBoxSolved.addEventListener('click', randomColor);
function randomColor() {
colorBoxSolved.style.background = '#' +
Math.floor(Math.random()*16777215).toString(16);
}
// SOURCE:
// https://css-tricks.com/snippets/javascript/random-hex-color/
</code></pre>
</div>
<div style="float:left;margin-top:100px;width:25%;">
<p id="colorboxsolved" style="width:200px;height:200px;background:lightblue;position:absolute;top:110px;left:820px;transition:0.1s;"></p>
</div>
</section>
<section>
<h2>Let's build a Mad Libs game!</h2>
</section>
<section>
<iframe src="madlibs/index.html" width="960" height="850" style="margin-top:-100px;"><a href="madlibs">Mad Libs demo</a></iframe>
</section>
<section>
<h2 style="font-size:1.4em;">What do we need to build this app?</h2>
<!--
we ask the students to describe what each part does, as review:
-->
<h3 class="green fragment">HTML</h3>
<h3 class="green fragment">CSS</h3>
<h3 class="green fragment">JavaScript</h3>
</section>
<section>
<h2 style="font-size:1.5em;margin-bottom:60px;">What does our JavaScript code need to do for this app?</h2>
<p class="fragment" style="margin-bottom:40px;">Get user input from the HTML <code><input></code> elements</p>
<p class="fragment" style="margin-bottom:40px;">Combine user input with our Mad Lib story</p>
<p class="fragment">Show finished Mad Lib when user clicks the button</p>
<p class="fragment green" style="margin-top:70px;">...but how do we do all that??</p>
</section>
<section>
<h2 style="font-size:1.5em;">Today's JavaScript concepts:</h2>
<ul class="less-margin">
<li>Using your web browser's console</li>
<li>Variables</li>
<li>Data types, starting with strings</li>
<li>Combining strings (concatenation)</li>
<li>Linking JavaScript files to HTML files</li>
<li>Built-in JavaScript functions</li>
<!-- reminders for later:
show DOCUMENTATION resources!
console.log
getElementById
also use .value and .innerHTML for Mad Libs
-->
<li>Creating your own functions</li>
<li>Using JavaScript to access HTML elements</li>
<li>JavaScript event listeners</li>
<!-- reminders for later:
show DOCUMENTATION resources, list of events!
...mind BLOWN: functions can take other functions as parameters!
-->
</ul>
</section>
<section>
<h3 style="margin-bottom: 30px;margin-top:-50px;">How to open your web browser's JavaScript console:</h3>
<ul>
<li><strong>Chrome:</strong>
<ul><li style="margin-top:20px;">
<strong><em>Windows:</em></strong> Ctrl + Shift + <strong>J</strong>
</li>
<li>
<strong><em>Mac:</em></strong> Cmd + Opt + <strong>J</strong>
</li>
</ul>
</li>
<li><strong>Firefox:</strong>
<ul><li style="margin-top:20px;">
<strong><em>Windows:</em></strong> Ctrl + Shift + <strong>K</strong>
</li>
<li>
<strong><em>Mac:</em></strong> Cmd + Opt + <strong>K</strong>
</li>
</ul></li>
<li><strong>Safari / <em>Mac:</em></strong> Cmd + Opt + <strong>C</strong>
<ul><li style="margin-top:20px;">
<em>Note: first you may need to enable web inspector (<a href="https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/GettingStarted/GettingStarted.html">see instructions here</a>)</em>
</li></ul>
</li>
</ul>
</section>
<section>
<h2>So what are variables?</h2>
<p>A variable is a symbolic name for a piece of information that can change over time.</p>
<pre><code class="javascript big" data-trim contenteditable="">
var myName = 'Sally Awesomepants';
</code></pre>
<p>Try it out! Copy and paste this code into your console. To run your code, hit enter.</p>
<!--
VERBAL instructions:
- note that now it's saved in memory, even if it hasn't done anything with the var yet!
- then try running "myName" and it will print it back out for you
-->
</section>
<section>
<h2 style="font-size:1.5em;">Important notes on variables:</h2>
<ul>
<li>Use the <code>var</code> keyword to define a <em>new</em> variable</li>
<li>New variables need to have unique names</li>
<li>Variable names are CASE SENSITIVE, cannot contain spaces, and cannot begin with numbers</li>
<li>Bonus link: <a href="http://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names" target="_blank">"What characters are valid for JavaScript variable names?" (the answer might surprise you!)</a></li>
</ul>
</section>
<section>
<h2>Creating new variables</h2>
<p>Variables can be created with an initial value or without any value at all (<code>undefined</code>):</p>
<pre><code class="javascript big" data-trim contenteditable="">
var favoriteAnimal = 'penguin';
var favoriteColor;
</code></pre>
<p>Try it out! Run this code, then type each variable name (don't forget to press "enter"!) and the console will show their values.</p>
</section>
<section>
<h2 style="font-size:1.5em;">Changing the value of a variable</h2>
<p>After a variable has been created, you can change it <em>without</em> using the <code>var</code> keyword:
<pre><code class="javascript big" data-trim contenteditable="">
var favoriteFood = 'salad';
// Actually, I changed my mind!
favoriteFood = 'pizza';
</code></pre>
<p>Try it yourself in the console!</p>
</section>
<section>
<h2>JavaScript comments</h2>
<p>Comments are ignored by the computer but very useful for us humans to keep our code organized:</p>
<pre style="font-size:0.6em;"><code class="javascript" data-trim contenteditable="">
// This is a single-line comment.
/* This is a multi-line comment!
Comments help us humans stay sane. You can use them for:
- notes to explain how your code works
- to-do lists and reminders
- links to sources or references, like this one about JS comments:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Comments
*/
</code></pre>
<p>Single-line comments start with <code class="green bold">//</code></p>
<p>Multi-line comments start with <code class="green bold">/*</code> and end with <code class="green bold">*/</code></p>
</section>
<section>
<h2>Data types</h2>
<p>Variables can contain different types of data, like strings, numbers, and more!</p>
<pre><code class="javascript" data-trim contenteditable="">
var adjective = 'purple';
var favoriteNumber = 3;
</code></pre>
<p class="small">The computer stores everything as 1s and 0s, but it <em>interprets</em> the 1s and 0s differently depending on what data type they are.</p>
<p class="small" style="margin-top:30px;">For example: "0001" might represent the number 1, the letter A, the value TRUE, or something else!</p>
</section>
<section>
<h2 style="font-size:1.5em;">A chunk of text is called a <strong>string</strong></h2>
<p>Strings are a bunch of symbols <em>strung</em> together into a sentence, just like alphabet beads on a necklace.</p>
<!--
LATER: section on converting data types:
<li class="fragment highlight-current-blue">If a string contains a number (<code>var myString = "5"</code>), the computer treats it as text.</li>
Also later:
- escaping strings, special characters
-->
</ul>
<img src="img/letter-beads.jpg" class="noborder">
<p>Strings are always surrounded by quote marks, either single (<code class="green bold">' '</code>) or double (<code class="green bold">" "</code>).</p>
<p class="small">It doesn't matter which you choose, as long as the opening and closing quote marks match!</p>
<p><cite>Photo by <a href="https://www.flickr.com/photos/smkybear/2066237409" target="_blank">Wendy (smkybear) on Flickr</a></cite></p>
</section>
<section>
<h2 style="font-size:1.2em;">What if your string contains a quote mark?</h2>
<p>Since strings are always surrounded by quote marks, if your string also <em>contains</em> a quote mark,<br> you'll run into trouble!</p>
<pre><code class="javascript" data-trim contenteditable="">
var brokenString = 'I think I'm doing this wrong!';
</code></pre>
<p>Try it out in the console. Did you get an error?</p>
<p>Let's see how to fix it...</p>
</section>
<section>
<h2 style="font-size:1.4em;">Solution 1: Use different quote marks</h2>
<p>You can use double (<code class="green bold">" "</code>) quote marks to surround a string containing a single quote:</p>
<pre><code class="javascript" data-trim contenteditable="">
var fixedString1 = "I think I'm doing this right!";
</code></pre>
<p>Or vice versa, you can use single quotes (<code class="green bold">' '</code>) to surround a string containing a double quote:</p>
<pre><code class="javascript" data-trim contenteditable="">
var fixedString2 = 'I shouted "Yay!" when it worked.';
</code></pre>
</section>
<section>
<h2 style="font-size:1.4em;">Solution 2: Escaping characters</h2>
<p>You can <strong>escape</strong> special characters like quote marks by adding a backslash (<code class="green bold">\</code>) in front of them:</p>
<pre style="margin: 50px 0;"><code class="javascript" data-trim contenteditable="">
var fixedString3 = 'Yay, I\'m escaping strings!';
</code></pre>
<p>Escape notation is also used for printing special characters like tab (<code class="green bold">\t</code>) and new line (<code class="green bold">\n</code>).
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" target="_blank">See more on strings in this reference page</a>.</p>
</section>
<section>
<h2>Connecting strings together</h2>
<p>So you've already defined some strings like <code>favoriteAnimal</code> and <code>favoriteFood</code>, but how can you combine them into a sentence?</p>
<!--
ASK: if you were inventing your own programming language, how would you do it?
Get a couple volunteers to say what they would type, before showing answer.
-->
<pre class="fragment"><code class="javascript" data-trim contenteditable="">
var sentence = 'My name is ' + myName + ' and I rock!';
</code></pre>
<p class="fragment">Try it! Run this code in your console. Then type "sentence" and hit enter, and you'll see the output:<br> <b>I am Sally Awesomepants and I rock!</b></p>
<p class="fragment small blue" style="margin-top:40px;"><strong>Note:</strong> The console remembers the <code>myName</code> variable that you defined earlier, because it's saved in memory until you close your browser tab. Neat, huh?</p>
</section>
<section>
<p style="margin-bottom:60px;">Now run this code in your console:</p>
<pre><code class="javascript" data-trim contenteditable="">
var madLib = 'Yesterday I found a ' + adjective + ' ' + favoriteAnimal + ' with ' + favoriteNumber + ' eyes eating ' + favoriteFood + ' on my dining table and writing JavaScript on my computer! It wrote some code that concatenated strings, so I ran the code and the ouput was: ' + sentence;
</code></pre>
<p style="margin-top:60px;">After running the code, remember to type <code>madLib</code> and hit enter to see the value of the madLib string!</p>
</section>
<section>
<h2 style="font-size:1.3em;margin-bottom:60px;">Combining strings = concatenation</h2>
<p>The fancy technical term for combining strings is called <strong class="green">concatenation</strong>. Say that ten times fast!</p>
<p style="margin-top:60px;">When used with strings, the <strong>+</strong> symbol is called the <strong>concatenation operator</strong>.</p>
</section>
<section>
<h2 style="font-size:1.4em;margin-top:-35px;">Challenge: Write your own Mad Lib</h2>
<!--
Now we're each going to make our own Mad Lib and share it with each other.
DEMONSTRATE creating a new one based on the template and pasting into the Google doc.
-->
<ol class="medium-margin">
<li>Go to this website: <a href="https://repl.it/languages/javascript" target="_blank">repl.it/languages/javascript</a></li>
<li>Create your own Mad Lib story based on this example: <a href="https://repl.it/EArM" target="_blank">repl.it/EArM</a></li>
<li>Click "Run" at the top to test it. Make sure your code includes <code>console.log(madLib);</code> at the end!</li>
<li>When you're done, click "Save" at the top and when it asks you to log in, click "continue as Anonymous" (the small text at the bottom of the popup)</li>
<li>Click "Share" at the top, copy the link, and paste it into here with your name: <a href="https://goo.gl/ELnYzm" target="_blank">goo.gl/ELnYzm</a></li>
<li>Try each others' Mad Libs and have a good laugh!</li>
</ol>
</section>
<section>
<h1>JavaScript Files</h1>
</section>
<section>
<h2 style="font-size:1.5em;">Let's set up our project</h2>
<ol class="medium-margin">
<li>Create a new folder named <code>madlibs</code> somewhere easy to find (maybe on your Desktop)</li>
<li>Open up your text editor (like <a href="https://atom.io/" target="_blank">Atom</a> or <a href="https://www.sublimetext.com/" target="_blank">Sublime</a>)</li>
<li>Create a new HTML file called <code>index.html</code> and save it in your madlibs folder</li>
<li>Create a new CSS file called <code>style.css</code> and save it in your madlibs folder</li>
<li>Create a new JavaScript file called <code>madlibs.js</code> and save it in your madlibs folder</li>
</ol>
</section>
<section>
<h2 style="font-size:1.5em;">Copy this into your index.html file:</h2>
<pre style="font-size:0.5em;"><code class="html" data-trim contenteditable="">
<!DOCTYPE html>
<html>
	<head>
		<title>Mad Libs! (Girl Develop It LA - Intro to JavaScript)</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width">
		<link href="style.css" rel="stylesheet" type="text/css">
		<link href="https://fonts.googleapis.com/css?family=Waiting+for+the+Sunrise" rel="stylesheet">
	</head>
	<body>
		<h1>Mad Libs!</h1>
		<div id="madlib"></div>
		<strong>Noun</strong> (ex: tree, person): <input type="text" id="noun" class="fill">
		<strong>Past Tense Verb</strong> (ex: jumped, ate): <input type="text" id="verb" class="fill">
		<strong>Adjective</strong> (ex: happy, purple): <input type="text" id="adjective" class="fill">
		<button id="submit">Create Mad Lib</button>
		<p class="small">Based on a practice project from <a href="https://dash.generalassemb.ly/">Dash by General Assembly</a></p>
		<script src="madlibs.js"></script>
	</body>
</html>
</code></pre>
</section>
<section>
<h2 style="font-size:1.5em;">Copy this into your style.css file:</h2>
<pre style="font-size:0.5em;"><code class="css" data-trim contenteditable="">
body {
font-family: 'Helvetica', 'Arial', sans-serif;
margin: 0 auto;
text-align: center;
padding: 0 20px;
max-width: 800px;
font-size: 25px;
background: #efefef;
}
button {
font-size: 25px;
font-weight: bold;
padding: 10px;
margin: 10px;
}
input.fill {
display: block;
width: 400px;
max-width:95%;
margin:0 auto;
margin-bottom: 20px;
box-sizing: border-box;
}
.fill {
font-size: 30px;
font-weight: bold;
font-family: 'Waiting for the Sunrise', cursive;
letter-spacing: 5px;
background: white;
color: red;
margin-left: 4px;
margin-right: 4px;
padding: 5px 15px;
}
#madlib {
font-size: 30px;
line-height: 2.5;
margin-top: 20px;
margin-bottom: 40px;
}
h1 {
font-size: 80px;
margin: 0;
color: red;
font-family: 'Waiting for the Sunrise', cursive;
font-weight: bold;
letter-spacing: 7px;
}
.small {
font-size:0.5em;
font-style: italic;
margin-top: 30px;
}
</code></pre>
</section>
<section>
<h2 style="font-size:1.4em;">How to link your JS and HTML files</h2>
<p class="small">In our HTML file, we use the <code><script></code> tag with the <code>src</code> attribute at the end of the <code>body</code> element to link it with our JavaScript file:</p>
<pre style="font-size:0.6em;"><code class="html" data-trim contenteditable="">
<html>
<!DOCTYPE html>
	<head>
<!-- other HTML here -->
	</head>
	<body>
<!-- and more HTML here -->
		<script src="madlibs.js"></script>
	</body>
</html>
</code></pre>
<p class="small">Note that here we assume <code>madlibs.js</code> is in the same folder as the HTML file that's linking to it.</p>
</section>
<section>
<h2>JavaScript Functions</h2>
</section>
<section>
<h2 style="font-size:1.4em;">Meet your first built-in function: <code>console.log</code>!</h2>
<p>Paste this code into your <code>madlibs.js</code> file, save it, and then open <code>index.html</code> in your web browser:</p>
<pre><code class="javascript" data-trim contenteditable="">
console.log('Hello world!');
</code></pre>
<p>Now open the browser console. You should see the message <code>"Hello world!"</code></p>
</section>
<section>
<p>The <code>console.log</code> function displays information in the console. Use it to confirm that your code is doing what you think it's doing!</p>
<img src="img/console-log-all-the-things.jpg" class="noborder" style="margin:30px 0;">
<p>This <em>log</em> of information is similar to the <a target="_blank" href="https://en.wikipedia.org/wiki/Logbook">logbooks</a> used by ship captains to track important events.</p>
</section>
<section>
<p>Fun fact: most web browsers support fancy customizations, like styling the messages with CSS!</p>
<img src="img/console-css.png" style="margin:30px 0;">
<p>See more advanced tricks in <a target="_blank" href="https://developers.google.com/web/tools/chrome-devtools/console/console-write">Chrome's documentation</a></p>
</section>
<section>
<h2>So what are functions?</h2>
<p>A function is a reusable chunk of code that performs a particular action.</p>
<img src="img/function-machine.png" class="noborder">
<p>In mathematics, a function transforms input(s) into output. But for JavaScript functions, the inputs and output are optional.</p>
<!-- <p class="small">Programming languages typically provide a bunch of built-in functions (like <code>console.log</code>), but you can also create your own functions!</p> -->
</section>
<section>
<h2>Running functions</h2>
<p>To run or <em>call</em> a function, type its name followed by<br> <code class="green bold">( );</code> with any inputs listed inside the parentheses:</p>
<pre><code class="javascript" data-trim contenteditable="">
// One string input:
console.log('I am a string');
// Three string inputs:
console.log('string1', 'string2', 'string3');
// Or no inputs at all:
console.log();
</code></pre>
<p>Functions in JavaScript can take zero, one, or more inputs separated by commas.</p>
</section>
<section>
<h2>Function inputs</h2>
<p>Any variable or data type can be used as input for a function, and you can mix and match:</p>
<pre><code class="javascript" data-trim contenteditable="">
var favoriteNumber = 5;
// Using a variable containing a number as input:
console.log(favoriteNumber);
var animal = 'penguins';
// You can mix and match data types:
console.log('I found ', favoriteNumber, animal);
</code></pre>
<p>JavaScript functions can even take other functions as their inputs! But more on that later.</p>
</section>
<section>
<h2 style="font-size:1.4em;">Defining your own custom functions</h2>
<p>JavaScript has lots of built-in functions, but you can also create your own!</p>
<pre><code class="javascript" data-trim contenteditable="">
// Defining a function called shout:
function shout (phrase) {
console.log(phrase + '!!!');
}
// Running (or calling) the function:
shout('I love JavaScript');
shout('Functions rock');
</code></pre>
<p>Try it out! First you <strong>define</strong> your function, and then you can <strong>call</strong> it as many times as you want.</p>
</section>
<section>
<h2 style="font-size:1.4em;">Parts of a function definition</h2>
<pre style="font-size:1.1em;">
<span class="fragment highlight-current-red" data-fragment-index="0">function</span> <span class="fragment highlight-current-red" data-fragment-index="1">shout</span> <span class="fragment highlight-current-red" data-fragment-index="2">(phrase)</span> <span class="fragment highlight-current-red" data-fragment-index="3">{
console.log(phrase + '!!!');
}</span>
</pre>
<!-- <p><span class="fragment highlight-current-red" data-fragment-index="0"><span class="bold">1)</span> the <code>function</code> keyword</span>, <span class="fragment highlight-current-red" data-fragment-index="1"><span class="bold">2)</span> the name of the function</span>, <span class="fragment highlight-current-red" data-fragment-index="2"><span class="bold">3)</span> parentheses containing optional inputs (parameters)</span>, and <span class="fragment highlight-current-red" data-fragment-index="3"><span class="bold">4)</span> curly brackets containing the body of the function or the code that it will run.</span></p> -->
<ol class="less-margin">
<li><span class="fragment highlight-current-red" data-fragment-index="0">The <code>function</code> keyword</span></li>
<li><span class="fragment highlight-current-red" data-fragment-index="1">The name of the function (follow same naming rules as variables)</span></li>
<li><span class="fragment highlight-current-red" data-fragment-index="2">Parentheses containing optional inputs (called parameters)</span></li>
<li><span class="fragment highlight-current-red" data-fragment-index="3">Curly brackets containing the code that it will run (called the function body)</span></li>
</ol>
</section>
<section>
<p>Defining a function with multiple <em>parameters</em>:</p>
<pre><code class="javascript" data-trim contenteditable="">
// Defining a function with 3 parameters:
function displayThreeWords (word1, word2, word3) {
console.log(word1 + ' ' + word2 + ' ' + word3);
}
</code></pre>
<p>Running the function with multiple <em>arguments</em>:</p>
<pre><code class="javascript" data-trim contenteditable="">
// Running the function with 3 arguments:
displayThreeWords('pineapple', 'motorboat', 'toothbrush');
</code></pre>
</section>
<section>
<h2>Parameters vs arguments</h2>
<!-- <ul>
<li>A <span class="green bold">parameter</span> refers to a function's input regardless of any specific value, like when we <strong>define</strong> the function.</li>
<li>An <span class="blue bold">argument</span> refers to the specific input used in a particular case, like when we <strong>call</strong> the function.</li>
</ul> -->
<p style="margin-bottom:30px;"><span class="bold">Here's a handy way to remember it:</span></p>
<ul class="less-margin">
<li>The <span class="green bold">p</span>arameter is the <span class="green bold">p</span>arking space</li>
<li>The <span class="blue bold">a</span>rgument is the <span class="blue bold">a</span>utomobile</li>
</ul>
<pre><code class="javascript" data-trim contenteditable="">
// 3 parameters, don't know the values yet:
function displayThreeWords (word1, word2, word3) {
console.log(word1 + ' ' + word2 + ' ' + word3);
}
// 3 arguments, specific values:
displayThreeWords('pineapple', 'motorboat', 'toothbrush');
</code></pre>
</section>
<section>
<h2>Returning outputs</h2>
<p>To get an output from your function, use the <code>return</code> keyword followed by the output value:</p>
<pre><code class="javascript" data-trim contenteditable="">
// Define a function with 1 parameter and an output:
function shout (phrase) {
return phrase + '!!!';
}
// Run the function and save the output to a variable