-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
1095 lines (901 loc) · 23.1 KB
/
script.js
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
if (1 === 1) {
console.log("it's true");
}
// example 2
//performence reivew
// 3 - superstar
// 2- meets expectations
// 1 - needs improvement
let raiting = 3
if (raiting === 3) {
let result = ('you are a superstar');
console.log(result)
}
else if (raiting === 2) {
let result = ('meets expectations')
console.log(result)
}
else if (raiting === 1) {
let result = ('needs improvement')
console.log(result)
}
else {
console.log('invalid response')
}
//example 3
let num = 37;
if (num % 2 !== 0) {
console.log('odd number');
}
//Example 3
let highScore = 1430;
let userScore = 1200;
if (userScore >= highScore) {
console.log(`Congrats, you have the new high score of ${userScore}`)
}
else {
console.log(`Good Game. Your score of ${userScore} did not beat the high score of ${highScore} `)
}
// NESTING
// Example 4
let password = 'hello kitty'
if (password.length >= 6) {
if (password.indexOf(' ') === -1) {
console.log('Valid Password')
} else {
console.log('password is long enough but cannot contain spaces')
}
}
else {
console.log("Password must be longer!")
}
// Example 5
let mystery = 9;
if (mystery) {
console.log('TRUTHY')
}
else {
console.log('FALSY')
}
//Example 6
let loggedInUSer = null;
if (loggedInUSer) {
console.log('YOU ARE LOGGED IN!');
}
else {
console.log(' PLEASE LOG IN!');
}
//Logical AND/OR/NOT
// Example 7
let password1 = 'taco tuesday'
if (password1.length >= 6 && password.indexOf(' ') === -1) {
console.log("Valid Password!");
}
else {
console.log('INVALID PASSWORD!')
}
// Example 8
let password2 = "chickenGal"
if (password2.length >= 8 && password2.indexOf(' ') === -1) {
console.log("WOW! you finally got the password right!");
}
else {
console.log('you got the password wrong again.')
}
//Example 9
let num1 = 5;
if (num1 >= 1 && num1 <= 10) {
console.log('Number is between 1 and 10');
}
else {
console.log('please guess a number between 1 and 10')
}
// Logical OR
// only 1 side needs to be true
//Example 9
let age = 10;
if (age < 6 || age >= 65) {
console.log('Yay! You get in for free!')
}
else {
console.log('You have to pay $10 for entry.')
}
//Example 10
//checking for shade of purple
let color = 'purple';
if (color === 'purple' || color === 'lilac' || color === 'violet') {
console.log('Great Choice!');
}
else {
console.log('The only color to matter is a shade of purple!')
}
// Not Operator
// Returns true if something is false
// Example 11
// if there isnt a logged in user do something
let loggedInUser1;
if (!loggedInUser1) {
console.log('GET OUT OF HERE!');
}
// Example 12
let flavor = 'apple'
if (flavor !== 'grape' && flavor !== 'cherry') {
console.log('WE DONT HAVE THAT FLAVOR!');
}
if (!(flavor === 'grape' || flavor === 'cherry')) {
console.log('We only have grape and cherry!')
}
// The Switch Statement
// Example 13
let day = 1;
// if (day === 1) {
// console.log ('MONDAY');
// }
// else if (day === 2) {
// console.log('Tuesday');
// }
// else if (day === 3) {
// console.log('Wednesday');
// }
// else if (day === 4) {
// console.log('Thursday');
// }
// else if (day === 5) {
// console.log('Friday');
// }
// else if (day === 6) {
// console.log('Saturday');
// }
// else if (day === 7) {
// console.log('Sunday');
// }
// else {
// console.log('INVALID DAY')
// }
switch (day) {
case 1:
console.log('MONDAY');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
break;
case 4:
console.log('Thursday');
break;
case 5:
console.log('Friday');
break;
case 6:
console.log('Saturday');
break;
case 7:
console.log('Sunday');
break;
default:
console.log('incorrect input')
}
let emoji = 'lips'
switch (emoji) {
case 'sad face':
case 'happy face':
console.log('yellow');
break;
case 'eggplant':
console.log('purple');
break;
case 'lips':
case 'heart':
console.log('red');
break;
// deafult is needed for incorrect input
}
//Ternary Operator
// condition ? expIfTrue: expIfFalse
//Example 14
let num2 = 7;
if (num2 === 7) {
console.log('lucky!')
}
else {
console.log('Unlucky!')
}
num2 === 7 ? console.log('lucky!') : console.log('Unlucky!')
//Example 15
let status = 'offline';
let status1 = 'offline';
let color1;
if (status === 'offline') {
color1 = 'red'
} else {
color1 = 'green'
}
let color2 = status1 === 'offline' ? 'red' : 'green'; {
console.log(color2)
}
//Arrays
//to make an array the [] is like a nempty pill box
let students = [];
let shoppingList = ['cheese', 'cereal', 'ice']
console.log(shoppingList)
let lotto = [45, 12, 23, 25, 34];
console.log(lotto)
let myCollection = [12, 'dog', true, null, NaN]
console.log(myCollection)
let colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// How to access information within the Array 1. colors.length; colors[1]; or colors[colors.length-1]
let colors1 = ['red', 'orange', 'green', 'yellow'];
colors[0] = 'red';
colors[2] = 'yellow';
colors[3] = 'green';
colors[4]; //undefined
colors[4] = 'blue';
// the above works but not when you don't know how many items there are. like if a user wanted to add another color from the web page.
colors1[colors1.length] = 'Purple'; // this adds to the end of the array
console.log(colors1);
///////////////////////// ARAY METHODS ////////////////////////////
let topSongs = [
'First Time Ever I Saw Your Face',
'God Only Knows',
'A Day In The Life',
'Life On Mars'
];
topSongs.push('Fortunate Son') //add something to the end
topSongs.pop() // removes the last item in the array and returns it
let dishesToDo = ['big platter'];
dishesToDo.unshift('large plate'); // add to start
dishesToDo.unshift('small plate');
dishesToDo.unshift('cereal bowl');
dishesToDo.unshift('mug');
dishesToDo.unshift('spoon');
console.log(dishesToDo);
dishesToDo.shift(); // Removes from start
dishesToDo.unshift('fork', 'knife'); // adds this entire chunk not each item indvidually
console.log(dishesToDo);
let fruits = ['apple', 'banana'];
let veggies = ['asparagus', 'brussel sprouts'];
let meats = ['steak', 'chicken breast'];
console.log(fruits.concat(veggies));
console.log(veggies.concat(fruits));
let allFood = fruits.concat(veggies, meats, fruits)
console.log(allFood)
let ingredients = [
'water',
'sugar',
'shrimp',
'flour',
'cheese'
]
// includes = true or false is the item in the array dirrect match
console.log(ingredients.includes('chocolate'));
if (ingredients.includes('flour')) {
console.log("I don't eat flour!");
}
// .indexof retunrs the first index of said value
console.log(ingredients.indexOf('sugar'));
// reverse reverses the current array
console.log(ingredients.reverse())
// join combines the contents of an array to a single string
console.log(ingredients.reverse().join(','));
let animals = ['shark', 'salmon', 'whale', 'bear', 'lizrd', 'tortoise'];
let swimmers = animals.slice(0, 3);
let mammals = animals.slice(2, 4);
let reptiles = animals.slice(4, 6);
console.log(swimmers)
console.log(mammals)
console.log(reptiles)
let quadruped = animals.slice(-3);
console.log(quadruped)
// creates a copy of an array
let copy = animals.slice
// to insert something after shark first argument to start removing or inserting. second how many should you delete and third how many should you add
animals.splice(1, 0, 'octopus');
console.log(animals);
animals.splice(3, 2);
console.log(animals);
animals.splice(3, 0, 'snake', 'frog');
console.log(animals);
animals.splice(0, 2, 'SHARK', 'OCTOPUS');
console.log(animals);
let people = ['Mrs. Robinson', 'Angie', 'Jolene',]
console.log(people.sort());
let nums = [34, 100000, 99, 56];
console.log(nums.sort())
let fruit = 'orange';
let color3 = fruit;
//Value type variable
let nums1 = [5, 6, 7, 8]
let otherNums = nums1
nums1.pop()
nums1.pop()
console.log(nums1)
const myEggs = ['brown', 'brown'];
myEggs.push('purple');
myEggs[0] = 'green';
console.log(myEggs)
const foods = ['milk'];
foods.push('chocolate');
foods.unshift('tortillas');
console.log(foods)
foods.pop()
foods.pop()
foods.pop()
console.log(foods)
// NESTED ARRAYS
const animalPairs = [
['doe', ['buck', 'stag']],
['ewe', 'ram'],
['peahen', 'peacock']
];
//To access 'ewe'
console.log(animalPairs[1][0]);
//To access 'buck'
console.log(animalPairs[0][1][0]);
//Updating a sub-array:
console.log(animalPairs[0][1].push('hart'));
//apparently some people call male deer 'harts'. idk.
console.log(animalPairs[0][2] = 'stag')
/////////////////////// OBJECTS ///////////////////////////
const fitbitData = [
308756,
1814,
211
]
const lucyFitbitData = [
12344,
1814,
211
]
// totalSteps -> 308727,
// totalMiles -> 211.7,
// avgCalorieBurn -> 5755,
// workoutsThisWeek -> '5 of 7',
// avgGoodSleep -> '2:13's
const fitBitData1 = {
totalSteps: 308727, // total steps is a key,
totalMiles: 211.7,
avgCalorieBurn: 5755,
workoutsThisWeek: '5 of 7',
avgGoodSleep: '2:13'
};
let data = {
a: 1,
b: 2,
c: 3
};
// Use [] when the key name is an invalid variable name ex. 76trombones
const numbers4 = {
100 : 'one hundred',
16 : 'sixteen',
'76 trombones' : 'Great song'
};
const palette = {
red : '#eb4d4b',
yellow : '#f9ca24',
blue : '#30336b'
};
let mysteryColor = 'yellow';
const userReviews = {};
userReviews['queenBee49'] = 4.5;
userReviews.mrSmith78 = 3.5;
userReviews['colt'] = 5
const student = {
firstName : 'David',
lastName : 'Jones',
strengths : [ 'Music', 'Art' ],
exams : {
midterm : 92,
final : 88
}
};
const avg = (student.exams.midterm + student.exams.final) / 2;
const shoppingCart = [
{
product : 'Jenga Classic',
price : 6.88,
quantity : 1
},
{
product : 'Echo Dot',
price : 29.99,
quantity : 3
},
{
product : 'Fire Stick',
price : 39.99,
quantity : 2
}
];
const game = {
player1 : {
username : 'Blue',
playingAs : 'X'
},
player2 : {
username : 'Muffins',
playingAs : 'O'
},
board : [ [ 'O', null, 'X' ],
[ 'X', 'O', 'X' ],
[ null, 'O', 'X' ] ]
};
const palette1 = {
red : '#eb4d4b',
yellow : '#f9ca24',
blue : '#30336b'
};
//Objects & Arrays are reference types
const palette2 = palette1;
//If we change one value...
palette2.green = '#ebf876';
//Both variables reflect that change...
palette.green; //"#ebf876"
palette2.green; //"#ebf876"
let nums2 = [1,2,3];
let mystery1 = [1,2,3];
let moreNums = nums2
//They 'look' the same, but refer to different arrays
nums2 === mystery1; // false
//These two arrays reference the exact same array, so we get true:
nums2 === moreNums; //true
const user = {
username : 'CherryGarcia8',
email : '[email protected]',
notifications : [ 'message', 'alert' ]
};
// //THIS WILL NOT WORK!
// if (user.notifications === []) {
// console.log('NO NEW NOTIFICATIONS!');
// }
// THIS VERSION DOES WORK!
if (!user.notifications.length) {
console.log('NO NEW NOTIFICATIONS!');
}
[1,2,3]
// LOOPS
// 3 pieces to a for loop:
// 1 - Variable declaration
// 2 - The condition
// 3 - Update the loop variable
// STEP 1: 'let i = 1' - start i at 1
// STEP 2: 'i <= 10' - run the loop as long as i <= 10
// STEP 3: 'i++' - add 1 to i each time through
for (let i = 1; i <= 10; i++) {
console.log('HELLO:', i);
}
// Counting by 3's:
for (let i = 1; i <= 10; i += 3) {
console.log('HELLO:', i);
}
// Printing first 20 perfect squares:
for (let num = 1; num <= 20; num++) {
console.log(`${num}x${num} = ${num * num}`);
}
// Counting DOWN from 200 by intervals of 25:
for (let i = 200; i >= 0; i -= 25) {
console.log(i);
}
console.log('AFTER THE LOOP!');
// 10 total Times
// 50 inital value
// 60 when to run the loop
// +1 how to change value each time
//////////////////// INFINITE LOOPS ////////////////
// DON'T RUN THIS!
// for (let i = 1; i !== 20; i += 2) {
// console.log(i);
// }
// THE ABOVE LOOP NEVER ENDS
// i starts at 1, and we add 2 each time
// 1,3,5,7,9,11,13,15,17,19,21,etc.
// i never hits 20, so the loop condition is always true
// Instead, write it this way:
for (let i = 1; i <= 20; i += 2) {
console.log(i);
}
for (let i=100; i>=0;i--){
console.log(i)
}
////////////// FOR LOOPS + ARRAYS ///////////////
// ======= EXAMPLE 1 ==========
// Printing each element in an array
const examScores = [ 98, 77, 84, 91, 57, 66 ];
for (let i = 0; i < examScores.length; i++) {
console.log(i, examScores[i]);
}
// ======= EXAMPLE 2 ==========
// Same idea, but with a more complex array
const myStudents = [
{
firstName : 'Zeus',
grade : 86
},
{
firstName : 'Artemis',
grade : 97
},
{
firstName : 'Hera',
grade : 72
},
{
firstName : 'Apollo',
grade : 90
}
];
for (let i = 0; i < myStudents.length; i++) {
let student = myStudents[i];
console.log(`${student.firstName} scored ${student.grade}`);
}
// ======= EXAMPLE 3 ==========
// Averaging all grades in myStudents array
let total = 0; //total will hold the sum of all grades
for (let i = 0; i < myStudents.length; i++) {
let student = myStudents[i];
total += student.grade; //add each grade to total
}
console.log(total / myStudents.length); //divide by number of students
// ======= EXAMPLE 4 ==========
// Reversing a string
const word = 'stressed';
let reversedWord = ''; //will hold reversed string
//Loop backwards over the string
for (let i = word.length - 1; i >= 0; i--) {
reversedWord += word[i]; //add each char to reversedWord
}
console.log(reversedWord);
// ================ Example 5 ====================
const animals1 = ['lions', 'tigers', 'bears'];
for (let animalpos = 0; animalpos < animals1.length; animalpos++){
console.log(animalpos, animals1[animalpos]);
}
// =============== NESTED LOOP =================
for (let i = 1; i <= 10; i++) {
console.log('OUTER LOOP:', i);
for (let j = 10; j >= 0; j -= 2) {
console.log(' INNER LOOP', j);
}
}
// EXAMPLE 2
// Sum all elements in our 2048 board
const gameBoard = [
[ 4, 32, 8, 4 ],
[ 64, 8, 32, 2 ],
[ 8, 32, 16, 4 ],
[ 2, 8, 4, 2 ]
];
let totalScore = 0
for (let i = 0; i < gameBoard.length; i++){
let row = gameBoard[i]
console.log(row)
for (j = 0; j < row.length; j++){
console.log(row[j]);
totalScore += row[j];
}
}
console.log("Total Score is " + (totalScore))
// =============== WHILE LOOPS ========================
for (let i = 0; i <= 5; i++) {
console.log(i);
}
//Recreating the above for loop w/ a while loop:
let j1 = 0;
while (j1 <= 5) {
console.log(j1);
j1++;
}
// ===== // Pick a target number we are trying to guess =====
// const target = Math.floor(Math.random() * 10);
// // ==== Make initial guess ====
// let guess = Math.floor(Math.random() * 10);
// // Continue looping while guess is not the target num
// while (guess !== target) {
// console.log(`Target: ${target} Guess: ${guess}`);
// // ====IMPORTANT!!====
// // ====Update the value of guess each time through the loop====
// guess = Math.floor(Math.random() * 10);
// }
// console.log(`Target: ${target} Guess: ${guess}`);
// console.log(`CONGRATS YOU WIN!!`);
const target = Math.floor(Math.random() * 10);
let guess;
//ANOTHER APPROACH TO THE PREVIOUS GUESSING 'GAME'
while (true) {
if (target === guess) break; //Break stops the loop in its tracks
console.log(`Target: ${target} Guess: ${guess}`);
guess = Math.floor(Math.random() * 10);
}
console.log(`Target: ${target} Guess: ${guess}`);
console.log(`CONGRATS YOU WIN!!`);
// ================================= FOR OF LOOP ============================================= //
let subreddits = [ 'soccer', 'popheads', 'cringe', 'books' ];
// With a standard for loop
for (let i = 0; i < subreddits.length; i++) {
console.log(subreddits[i]);
}
//Much cleaner with a for...of loop!
for (let sub of subreddits) {
console.log(sub);
}
//Works with other iterables, like strings!
for (let char of 'cockadoodledoo') {
console.log(char.toUpperCase());
}
const magicSquare = [ [ 2, 7, 6 ],
[ 9, 5, 1 ],
[ 4, 3, 8 ] ];
// Version using a for loop...
for (let i = 0; i < magicSquare.length; i++) {
let row = magicSquare[i];
let sum = 0;
for (let j = 0; j < row.length; j++) {
sum += row[j];
}
console.log(`${row} summed to ${sum}`);
}
// Much cleaner version using a for...of
for (let row of magicSquare) {
let sum = 0;
for (let num of row) {
sum += num;
}
console.log(`${row} summed to ${sum}`);
}
// EXAMPLE 2
// If you need the indices, use a traditional for loop!
const words1 = [ 'mail', 'milk', 'bath', 'black' ];
const words2 = [ 'box', 'shake', 'tub', 'berry' ];
for (let i = 0; i < words1.length; i++) {
//Access index i of both arrays
console.log(`${words1[i]}${words2[i]}`);
}
///// ======================== LOOPING OVER OBJECTS ============== //////
const movieReviews = {
Arrival : 9.5,
Alien : 9,
Amelie : 8,
'In Bruges' : 9,
Amadeus : 10,
'Kill Bill' : 8,
'Little Miss Sunshine' : 8.5,
Coraline : 7.5
};
// THIS DOES NOT WORK!
// OBJECTS ARE NOT ITERABLE (can't use a for...of loop)
// for (let x of movieReviews) {
// console.log(x);
// }
// We CAN iterate over the keys of an object
for (let movie of Object.keys(movieReviews)) {
console.log(`You rated ${movie} - ${movieReviews[movie]}`);
}
// We can also iterate over the values
// To calculate the average movie rating:
const ratings = Object.values(movieReviews);
let total1 = 0;
for (let r of ratings) {
total1 += r;
}
let avg1 = total1 / ratings.length;
console.log('Average Rating: ', avg);
// ========================== FOR IN LOOPS ==============================
// for (variavle in object) {
// statement
// }\
const jeopardyWinnings = {
regularPlay: 2522700,
watsonChallenge: 300000,
tournamentOfChampions: 5000000,
battleOfTheDecades: 1000000
};
for (let prop in jeopardyWinnings){
console.log(prop);
console.log(jeopardyWinnings[prop]);
}
let total2 = 0;
for (let prop in jeopardyWinnings){
total2 += jeopardyWinnings[prop];
}
console.log(`Ken Jennings Total Earnings: ${total2}`)
// looping over properties not their values. The key is a number
// Don't use below method
for(let k in [88,99,77,66]){
console.log(k);
}
/// ================================ FUNCTIONS =================================== ///
// STEP 1: Define the function:
function grumpus() {
console.log('ugh...you again...');
console.log('FOR THE LAST TIME...');
console.log('LEAVE ME ALONE!!!');
}
// STEP 2: Call the function:
grumpus();
grumpus();
grumpus();
for (let i = 0; i < 1; i++) {
grumpus();
}
// ====== DICE ROLL FUNCTION ======= //
// Define our first function
function rollDie() {
// Pick a random number from 1-6
// - Math.random() gives us a decimal from 0-1
// - We multiply by 6, so now we have a random number between 0 up to 6 (but not including 6). Something likee 3.490823 or 5.991234
// - Then we floor to remove the decimal, leaving us with a whole number from 0-5
//- Lastly we add one, to get a number between 1 and 6
let roll = Math.floor(Math.random() * 6) + 1;
console.log(`Rolled: ${roll}`);
}
// We can call functions inside of other functions!
function throwDice() {
for (i=0; i<=6; i++) {
rollDie();
}
}
//Call it!
throwDice();
//// ============ INTRO TO ARGUMETS ======================= ///
// function greet() {
// console.log('Hi');
// }
// greet now expects a single argument
function greet(nickname) {
console.log(`Hi, ${nickname}!`);
}
greet('Sansa');
greet('Ramsay');
// EXAMPLE 2
function rollDie() {
let roll = Math.floor(Math.random() * 6) + 1;
console.log(`Rolled: ${roll}`);
}
// We can now specify how many dice to roll!
function throwDice(numRolls) {
for (let i = 0; i < numRolls; i++) {
rollDie();
}
}
throwDice(2);
throwDice(3);
// ================================= multiple arguments ===============================
function square(num) {
console.log(num * num);
}
function sum(x, y) {
console.log(x + y);
}
function divide(a, b) {
console.log(a / b);
}
// ============================= RETURNING A VALUE =============================================
// No return!
function add(x, y) {
console.log(x + y);
}
// you can only return one value
// This version returns the sum of x & y;
function add(x, y) {
return x + y;
}
// We can capture the return value:
const total3 = add(4, 9); //13
function square(x) {
return x * x;
console.log('ALL DONE!'); //this NEVER runs;
}
// One way of writing this function
function isPurple(color) {
if (color.toLowerCase() === 'purple') {
return true;
}
else {
return false;
}
}
// We don't need the else!
function isPurple(color) {
if (color.toLowerCase() === 'purple') {
return true;
}
return false;
}
// An even shorter way!
function isPurple(color) {
return color.toLowerCase() === 'purple';
}
function containsPurple(arr) {
for (let color of arr) {
if (color === 'purple' || color === 'magenta') {
return true;
}
}
return false;
}
//////// ================================ PRACTICE ==================================== //////
// Write a isValidPassword function
// It accepts 2 arguments: password and username
// Password must:
// - be at least 8 characters
// - cannot contain spaces
// - cannot contain the username
// If all requirements are met, return true.
//Otherwise: false