-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101-exercises
1647 lines (1274 loc) · 54.2 KB
/
101-exercises
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
"use strict"; // leave this line here :)
// Welcome to 101 Exercises in JS
// If you get "ReferenceError: someVariableName is not defined", that means you need to create a variable
// Exercise #0, Example Problem:
// Example problem setup: Create a variable named doingJSRightNow and assign it the boolean true.
// The line below creates the variable named doingJSRightNow and assigns the boolean value true
// To complete Exercise #0, uncomment the following line of JS
var doingJSRightNow = true
// The lines below will test your answer. If you see an error, then it means that your answer is incorrect or incomplete.
assert(doingJSRightNow, true, "a variable holding a true boolean value should be equal to true");
addToDone("Exercise 0 is correct");
// Exercise 1
// On the line below, create a variable named onMarsRightNow and assign it the boolean value of false
var onMarsRightNow = false
assert(onMarsRightNow, false, "If you see a Reference Error, be sure to create the variable and assign it a value.");
addToDone("Exercise 1 is correct.");
// Exercise 2
// Create a variable named fruits and assign it an array of strings containing the following fruits.
// mango, banana, guava, kiwi, and strawberry.
var fruits = ['mango', 'banana', 'guava', 'kiwi', 'strawberry']
assert(fruits, ["mango", "banana", "guava", "kiwi", "strawberry"], "Ensure the variable contains all the strings in the provided order");
addToDone("Exercise 2 is correct.");
// Exercise 3
// Create a variable named vegetables and assign it an array of fruits containing the following vegetable names as strings:
// eggplant, broccoli, carrot, cauliflower, and zucchini
var vegetables = ['eggplant', 'broccoli', 'carrot', 'cauliflower', 'zucchini']
assert(vegetables, ["eggplant", "broccoli", "carrot", "cauliflower", "zucchini"], "Ensure the variable contains all the strings in the provided order");
addToDone("Exercise 3 is correct.");
// Exercise 4
// Create a variable named numbers and assign it an array of numbers, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert(numbers, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "Ensure the variable contains the numbers 1-10 in order.");
addToDone("Exercise 4 is correct.");
// Exercise 5
// Add the string "tomato" to the end of the fruits array.
// *Hint* Recommend finding and using a built-in JS operation to add to an array rather than recreating the array.
fruits.push("tomato")
assert(fruits, ["mango", "banana", "guava", "kiwi", "strawberry", "tomato"], "Ensure the variable contains all the strings in the right order");
addToDone("Exercise 5 is correct");
// Exercise 6
// Given the following assignment of the vegetables array, add "tomato" to the end of the array.
// Recommend using the built-in JS operation to add to an array.
vegetables.push("tomato")
assert(vegetables,["eggplant", "broccoli", "carrot", "cauliflower", "zucchini", "tomato"], "Ensure the variable contains all the strings in the provided order");
addToDone("Exercise 6 is correct")
// Exercise 7
// Given the array of numbers defined below, reverse the array of numbers that you created above.
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.reverse()
assert(numbers, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], "An Error means that the answer is incorrect.")
addToDone("Exercise 7 is correct")
// Exercise 8
// Sort the vegetables in alphabetical order. Recommend finding a way to sort the array with a built-in method
vegetables.sort()
assert(vegetables, ['broccoli', 'carrot', 'cauliflower', 'eggplant', 'tomato', 'zucchini'])
addToDone("Exercise 8 is correct.")
// Exercise 9
// Write the code necessary to sort the fruits in reverse alphabetical order
fruits.sort()
fruits.reverse()
assert(fruits, ['tomato', 'strawberry', 'mango', 'kiwi', 'guava', 'banana'])
addToDone("Exercise 9 is correct.")
// Exercise 10
// Write the code necessary to produce a single array that holds all fruits then all vegetables in the order as they were sorted above.
// Assign the result to a variable named fruitsAndVeggies.
// *hint* the search engine search here would be "how to combine two arrays in JavaScript", for example.
var fruitsAndVeggies = fruits
vegetables.forEach(function(veggie){
fruitsAndVeggies.push(veggie)
})
assert(fruitsAndVeggies, ['tomato', 'strawberry', 'mango', 'kiwi', 'guava', 'banana', 'broccoli', 'carrot', 'cauliflower', 'eggplant', 'tomato', 'zucchini'])
addToDone("Exercise 10 is correct")
// This function generates a random number that is both positive and even
function randomPositiveEvenNumber() {
var randomNumber = Math.ceil(Math.random() * 100) + 10;
if(randomNumber % 2 !== 0) {
return randomPositiveEvenNumber()
}
return randomNumber;
}
// this function generates a random number that is both positive and odd
function randomPositiveOddNumber() {
var randomNumber = Math.ceil(Math.random() * 100) + 10;
if(randomNumber % 2 === 0) {
return randomPositiveOddNumber();
}
return randomNumber;
}
// this function generates a random number that is both negative and even.
function randomNegativeEvenNumber() {
var randomNumber = Math.ceil(Math.random() * -100) - 10;
if(randomNumber % 2 === 0) {
return randomNumber;
}
return randomNegativeEvenNumber();
}
// this function generates a random number that is both negative and odd.
function randomNegativeOddNumber() {
var randomNumber = Math.ceil(Math.random() * -100) - 10;
if(randomNumber % 2 === 0) {
return randomNegativeOddNumber();
}
return randomNumber;
}
// The next 4 lines create variables that hold these generated random numbers
var positiveEvenNumber = randomPositiveEvenNumber()
var positiveOddNumber = randomPositiveOddNumber();
var negativeEvenNumber = randomNegativeEvenNumber();
var negativeOddNumber = randomNegativeOddNumber();
// Writing functions
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions for help with writing functions
// Be sure to return values from your function definitions. The assert statements will call/run your function(s) for you.
// Example function defintion:
// Write a sayHello function that adds the string "Hello, " to the beginning and "!" to the end of any given input.
function sayHello(name) {
return "Hello, " + name + "!";
}
assert(sayHello("Jane"), "Hello, Jane!");
assert(sayHello("Pat"), "Hello, Pat!");
assert(sayHello("Astrud"), "Hello, Astrud!");
assert(sayHello("June"), "Hello, June!");
assert(sayHello("World"), "Hello, World!");
// Heres another example function definition
// This plusTwo function takes in a variable and adds the number 2 to it.
function plusTwo(x) {
return x + 2;
}
assert(plusTwo(3), 5, "3 plus 2 is five")
assert(plusTwo(-2), 0, "-2 plus 2 is zero")
assert(plusTwo(0), 2, "zero plus 2 is two")
// Exercise 11
// Write a function definition for a function named addOne that takes in a number and returns that number plus one
function addOne(num){
return num + 1
}
assert(addOne(2), 3, "Ensure that the function is defined, named properly, and returns the correct value");
assert(addOne(0), 1, "Zero plus one is one.");
assert(addOne(positiveEvenNumber), positiveEvenNumber + 1, "Ensure that the function is defined, named properly, and returns the correct value");
assert(addOne(negativeOddNumber), negativeOddNumber + 1, "Ensure that the function is defined, named properly, and returns the correct value");
addToDone("Exercise 11 is correct.")
// Exercise 12
// Write a function definition named isPositive that takes in a number and returns true or false if that number is positive.
function isPositive(num){
return num > 0;
}
assert(isPositive(positiveOddNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isPositive(positiveEvenNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isPositive(negativeOddNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isPositive(negativeEvenNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
addToDone("Exercise 12 is correct.")
// Exercise 13
// Write a function definition named isNegative that takes in a number and returns true or False if that number is negative.
function isNegative(num){
return num < 0
}
assert(isNegative(positiveOddNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isNegative(positiveEvenNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isNegative(negativeOddNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isNegative(negativeEvenNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
addToDone("Exercise 13 is correct.")
// Exercise 14
// Write a function definition named isOdd that takes in a number and returns true or false if that number is odd.
function isOdd(num){
return (num % 2 !== 0);
}
assert(isOdd(positiveOddNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isOdd(positiveEvenNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isOdd(negativeOddNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isOdd(negativeEvenNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
addToDone("Exercise 14 is correct.")
// Exercise 15
// Write a function definition named isEven that takes in a number and returns true or false if that number is even.
function isEven(num){
return (num % 2 === 0);
}
assert(isEven(2), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isEven(positiveOddNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isEven(positiveEvenNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isEven(negativeOddNumber), false, "Ensure that the function is defined, named properly, and returns the correct value");
assert(isEven(negativeEvenNumber), true, "Ensure that the function is defined, named properly, and returns the correct value");
addToDone("Exercise 15 is correct.")
// Exercise 16
// Write a function definition named identity that takes in any argument and returns that argument's value. Don't overthink this one!
function identity(input){
return input
}
assert(identity(fruits), fruits, "Ensure that the function is defined, named properly, and returns the correct value");
assert(identity(vegetables), vegetables, "Ensure that the function is defined, named properly, and returns the correct value");
assert(identity(positiveOddNumber), positiveOddNumber, "Ensure that the function is defined, named properly, and returns the correct value");
assert(identity(positiveEvenNumber), positiveEvenNumber, "Ensure that the function is defined, named properly, and returns the correct value");
assert(identity(negativeOddNumber), negativeOddNumber, "Ensure that the function is defined, named properly, and returns the correct value");
assert(identity(negativeEvenNumber), negativeEvenNumber, "Ensure that the function is defined, named properly, and returns the correct value");
addToDone("Exercise 16 is correct.")
// Exercise 17
// Write a function definition named isPositiveOdd that takes in a number and returns true or false if the value is both greater than zero and odd
function isPositiveOdd(num){
return isPositive(num) && isOdd(num)
}
assert(isPositiveOdd(3), true, "Double check your syntax and logic" );
assert(isPositiveOdd(positiveOddNumber), true, "Double check your syntax and logic");
assert(isPositiveOdd(positiveEvenNumber), false, "Double check your syntax and logic");
assert(isPositiveOdd(negativeOddNumber), false, "Double check your syntax and logic");
assert(isPositiveOdd(negativeEvenNumber), false, "Double check your syntax and logic");
addToDone("Exercise 17 is correct.")
// Exercise 18
// Write a function definition named isPositiveEven that takes in a number and returns true or false if the value is both greater than zero and even
function isPositiveEven(num){
return isPositive(num) && isEven(num)
}
assert(isPositiveEven(4), true, "Double check your syntax and logic" );
assert(isPositiveEven(positiveOddNumber), false, "Double check your syntax and logic");
assert(isPositiveEven(positiveEvenNumber), true, "Double check your syntax and logic");
assert(isPositiveEven(negativeOddNumber), false, "Double check your syntax and logic");
assert(isPositiveEven(negativeEvenNumber), false, "Double check your syntax and logic");
addToDone("Exercise 18 is correct.")
// Exercise 19
// Write a function definition named isNegativeOdd that takes in a number and returns true or false if the value is both less than zero and odd.
function isNegativeOdd(num){
return isNegative(num) && isOdd(num)
}
assert(isNegativeOdd(-3), true, "Double check your syntax and logic" );
assert(isNegativeOdd(positiveOddNumber), false, "Double check your syntax and logic");
assert(isNegativeOdd(positiveEvenNumber), false, "Double check your syntax and logic");
assert(isNegativeOdd(negativeOddNumber), true, "Double check your syntax and logic");
assert(isNegativeOdd(negativeEvenNumber), false, "Double check your syntax and logic");
addToDone("Exercise 19 is correct.")
// Exercise 20
// Write a function definition named isNegativeEven that takes in a number and returns true or false if the value is both less than zero and even.
function isNegativeEven(num){
return isNegative(num) && isEven(num)
}
assert(isNegativeEven(-4), true, "Double check your syntax and logic" );
assert(isNegativeEven(positiveOddNumber), false, "Double check your syntax and logic");
assert(isNegativeEven(positiveEvenNumber), false, "Double check your syntax and logic");
assert(isNegativeEven(negativeOddNumber), false, "Double check your syntax and logic");
assert(isNegativeEven(negativeEvenNumber), true, "Double check your syntax and logic");
addToDone("Exercise 20 is correct.")
// Exercise 21
// Write a function definition named half that takes in a number and returns half the provided number.
function half(num){
return num/2
}
assert(half(4), 2);
assert(half(5), 2.5);
assert(half(positiveOddNumber), positiveOddNumber / 2);
assert(half(positiveEvenNumber), positiveEvenNumber / 2);
assert(half(negativeOddNumber), negativeOddNumber / 2);
assert(half(negativeEvenNumber), negativeEvenNumber / 2);
addToDone("Exercise 21 is correct.")
// Exercise 22
// Write a function definition named double that takes in a number and returns double the provided number.
function double(num){
return num * 2
}
assert(double(4), 8)
assert(double(5), 10)
assert(double(positiveOddNumber), positiveOddNumber * 2)
assert(double(positiveEvenNumber), positiveEvenNumber * 2)
assert(double(negativeOddNumber), negativeOddNumber * 2)
assert(double(negativeEvenNumber), negativeEvenNumber * 2)
addToDone("Exercise 22 is correct.")
// Exercise 23
// Write a function definition named triple that takes in a number and returns triple the provided number.
function triple(num){
return num * 3
}
assert(triple(4), 12);
assert(triple(5), 15);
assert(triple(positiveOddNumber), positiveOddNumber * 3);
assert(triple(positiveEvenNumber), positiveEvenNumber * 3);
assert(triple(negativeOddNumber), negativeOddNumber * 3);
assert(triple(negativeEvenNumber), negativeEvenNumber * 3);
addToDone("Exercise 23 is correct.")
// Exercise 24
// Write a function definition named reverseSign that takes in a number and returns the provided number but with the sign reversed.
function reverseSign(num){
return num * -1
}
assert(reverseSign(4), -4);
assert(reverseSign(-5), 5);
assert(reverseSign(positiveOddNumber), positiveOddNumber * -1);
assert(reverseSign(positiveEvenNumber), positiveEvenNumber * -1);
assert(reverseSign(negativeOddNumber), negativeOddNumber * -1);
assert(reverseSign(negativeEvenNumber), negativeEvenNumber * -1);
addToDone("Exercise 24 is correct.")
// Exercise 25
// Write a function definition named absoluteValue that takes in a number and returns the absolute value of the provided number
function absoluteValue(num){
return Math.abs(num)
}
assert(absoluteValue(4), 4);
assert(absoluteValue(-5), 5);
assert(absoluteValue(positiveOddNumber), positiveOddNumber);
assert(absoluteValue(positiveEvenNumber), positiveEvenNumber);
assert(absoluteValue(negativeOddNumber), negativeOddNumber * -1);
assert(absoluteValue(negativeEvenNumber), negativeEvenNumber * -1);
addToDone("Exercise 25 is correct.")
// Exercise 26
// Write a function definition named isMultipleOfThree that takes in a number and returns true or false if the number is evenly divisible by 3.
function isMultipleOfThree(num){
return (num % 3 === 0)
}
assert(isMultipleOfThree(3), true);
assert(isMultipleOfThree(15), true);
assert(isMultipleOfThree(9), true);
assert(isMultipleOfThree(4), false);
assert(isMultipleOfThree(10), false);
addToDone("Exercise 26 is correct.")
// Exercise 27
// Write a function definition named isMultipleOfFive that takes in a number and returns true or false if the number is evenly divisible by 5.
function isMultipleOfFive(num){
return (num % 5 === 0)
}
assert(isMultipleOfFive(3), false);
assert(isMultipleOfFive(15), true);
assert(isMultipleOfFive(9), false);
assert(isMultipleOfFive(4), false);
assert(isMultipleOfFive(10), true);
addToDone("Exercise 27 is correct.")
// Exercise 28
// Write a function definition named isMultipleOfBothThreeAndFive that takes in a number and returns true or false if the number is evenly divisible by both 3 and 5.
function isMultipleOfBothThreeAndFive(num){
return isMultipleOfThree(num) && isMultipleOfFive(num)
}
assert(isMultipleOfBothThreeAndFive(15), true);
assert(isMultipleOfBothThreeAndFive(45), true);
assert(isMultipleOfBothThreeAndFive(3), false);
assert(isMultipleOfBothThreeAndFive(9), false);
assert(isMultipleOfBothThreeAndFive(4), false);
addToDone("Exercise 28 is correct.")
// Exercise 29
// Write a function definition named square that takes in a number and returns the number times itself.
function square(num){
return Math.pow(num,2)
}
assert(square(3), 9);
assert(square(2), 4);
assert(square(9), 81);
assert(square(positiveOddNumber), positiveOddNumber * positiveOddNumber);
addToDone("Exercise 29 is correct.")
// Exercise 30
// Write a function definition named add that takes in two numbers and returns the sum.
function add(a,b){
return a + b
}
assert(add(3, 2), 5);
assert(add(10, -2), 8);
assert(add(5, 7), 12);
addToDone("Exercise 30 is correct.")
// Exercise 31
// Write a function definition named cube that takes in a number and returns the number times itself, times itself.
function cube(num){
return Math.pow(num , 3)
}
assert(cube(3), 27);
assert(cube(2), 8);
assert(cube(5), 125);
assert(cube(positiveOddNumber), positiveOddNumber * positiveOddNumber * positiveOddNumber);
addToDone("Exercise 31 is correct.")
// Exercise 32
// Write a function definition named squareRoot that takes in a number and returns the square root of the provided number
function squareRoot(num){
return Math.sqrt(num)
}
assert(squareRoot(4), 2.0);
assert(squareRoot(64), 8.0);
assert(squareRoot(81), 9.0);
addToDone("Exercise 32 is correct.")
// Exercise 33
// Write a function definition named subtract that takes in two numbers and returns the first minus the second argument.
function subtract(a, b){
return a-b
}
assert(subtract(8, 6), 2);
assert(subtract(27, 4), 23);
assert(subtract(12, 2), 10);
addToDone("Exercise 33 is correct.")
// Exercise 34
// Write a function definition named multiply that takes in two numbers and returns the first times the second argument.
function multiply(a, b){
return a * b
}
assert(multiply(2, 1), 2);
assert(multiply(3, 5), 15);
assert(multiply(5, 2), 10);
addToDone("Exercise 34 is correct.")
// Exercise 35
// Write a function definition named divide that takes in two numbers and returns the first argument divided by the second argument.
function divide(a,b){
return a / b
}
assert(divide(27, 9), 3);
assert(divide(15, 3), 5);
assert(divide(5, 2), 2.5);
assert(divide(10, 2), 5);
addToDone("Exercise 35 is correct.")
// Exercise 36
// Write a function definition named quotient that takes in two numbers and
// returns only the quotient first argument quotient by the second argument.
function quotient(a, b){
return Math.floor(a / b)
}
assert(quotient(27, 9), 3)
assert(quotient(5, 2), 2)
assert(quotient(10, 3), 3)
addToDone("Exercise 36 is correct.")
// Exercise 37
// Write a function definition named remainder that takes in two numbers and returns the remainder of first argument divided by the second argument.
function remainder (a, b){
return (a % b)
}
assert(remainder(3, 3), 0);
assert(remainder(5, 2), 1);
assert(remainder(7, 5), 2);
addToDone("Exercise 37 is correct.")
// Exercise 38
// Write a function definition named sumOfSquares that takes in two numbers, squares each number, then returns the sum of both squares.
function sumOfSquares( a, b){
return square(a) + square(b)
}
assert(sumOfSquares(3, 2), 13);
assert(sumOfSquares(5, 2), 29);
assert(sumOfSquares(2, 4), 20);
addToDone("Exercise 38 is correct.")
// Exercise 39
// Write a function definition named timesTwoPlusThree that takes in a number, multiplies it by two, adds 3 and returns the result.
function timesTwoPlusThree(num){
return num * 2 + 3
}
assert(timesTwoPlusThree(0), 3);
assert(timesTwoPlusThree(1), 5);
assert(timesTwoPlusThree(2), 7);
assert(timesTwoPlusThree(3), 9);
assert(timesTwoPlusThree(5), 13);
addToDone("Exercise 39 is correct.")
// Exercise 40
// Write a function definition named areaOfRectangle that takes in two numbers and returns the product.
function areaOfRectangle(a, b){
return a * b
}
assert(areaOfRectangle(1, 3), 3);
assert(areaOfRectangle(5, 2), 10);
assert(areaOfRectangle(2, 7), 14);
assert(areaOfRectangle(5.3, 10.3), 54.59);
addToDone("Exercise 40 is correct.")
// Exercise 41
// Write a function definition named areaOfCircle that takes in a number representing a circle's radius and returns the area of the circle
function areaOfCircle(radius){
return Math.PI * Math.pow(radius, 2)
}
assert(areaOfCircle(3), 28.274333882308138);
assert(areaOfCircle(5), 78.53981633974483);
assert(areaOfCircle(7), 153.93804002589985);
addToDone("Exercise 41 is correct.")
// Exercise 42
// Write a function definition circumference named circumference that takes in a number representing a circle's radius and returns the circumference.
function circumference(radius){
return 2 * Math.PI * radius
}
assert(circumference(3), 18.84955592153876);
assert(circumference(5), 31.41592653589793);
assert(circumference(7), 43.982297150257104);
addToDone("Exercise 42 is correct.")
// Exercise 43
// Write a function definition named isVowel that takes in value and returns true if the value is a, e, i, o, u in upper or lower case.
function isVowel(letter){
var vowelArray = ["a", "e", "i", "o", "u"]
return vowelArray.indexOf(letter.toLowerCase()) >= 0
}
assert(isVowel("a"), true);
assert(isVowel("U"), true);
assert(isVowel("banana"), false);
assert(isVowel("Q"), false);
assert(isVowel("y"), false);
addToDone("Exercise 43 is correct.")
// Exercise 44
// Write a function definition named hasVowels that takes in value and returns true if the string contains any vowels.
function hasVowels(input){
var strArray = input.toLowerCase().split("")
for (var i = 0; i< strArray.length; i++){
if(isVowel(strArray[i])) return true;
}
return false
}
assert(hasVowels("banana"), true);
assert(hasVowels("ubuntu"), true);
assert(hasVowels("QQQQ"), false);
assert(hasVowels("wyrd"), false);
addToDone("Exercise 44 is correct.")
// Exercise 45
// Write a function definition named countVowels that takes in value and returns the count of the nubmer of vowels in a sequence.
function countVowels(input){
var strArray = input.toLowerCase().split("")
var counter = 0
for (var i = 0; i< strArray.length; i++){
if(isVowel(strArray[i])) counter++;
}
return counter
}
assert(countVowels("banana"), 3)
assert(countVowels("ubuntu"), 3)
assert(countVowels("mango"), 2)
assert(countVowels("QQQQ"), 0)
assert(countVowels("wyrd"), 0)
addToDone("Exercise 45 is correct.")
// Exercise 46
// Write a function definition named removeVowels that takes in string and returns the string without any vowels
function removeVowels(input){
var tempArray = input.split("")
var strArray = input.toLowerCase().split("")
var newArray = []
for (var i = 0; i< strArray.length; i++){
if(isVowel(strArray[i]) == false ) newArray.push(tempArray[i]);
}
return newArray.join('')
}
assert(removeVowels("banana"), "bnn");
assert(removeVowels("ubuntu"), "bnt");
assert(removeVowels("mango"), "mng");
assert(removeVowels("QQQQ"), "QQQQ");
addToDone("Exercise 46 is correct.")
// Exercise 47
// Write a function definition named startsWithVowel that takes in string and true if the string starts with a vowel
function startsWithVowel(input){
return isVowel(input.charAt(0))
}
assert(startsWithVowel("ubuntu"), true);
assert(startsWithVowel("banana"), false);
assert(startsWithVowel("mango"), false);
addToDone("Exercise 47 is correct.")
// Exercise 48
// Write a function definition named endsWithVowel that takes in string and true if the string ends with a vowel
function endsWithVowel(input){
return isVowel(input.charAt(input.length-1))
}
assert(endsWithVowel("ubuntu"), true);
assert(endsWithVowel("banana"), true);
assert(endsWithVowel("mango"), true);
assert(endsWithVowel("spinach"), false);
addToDone("Exercise 48 is correct.")
// Exercise 49
// Write a function definition named startsAndEndsWithVowel that takes in string and returns true if the string starts and ends with a vowel
function startsAndEndsWithVowel(input){
return startsWithVowel(input) && endsWithVowel(input)
}
assert(startsAndEndsWithVowel("ubuntu"), true);
assert(startsAndEndsWithVowel("banana"), false);
assert(startsAndEndsWithVowel("mango"), false);
addToDone("Exercise 49 is correct.")
// Exercise 50
// Write a function definition named first that takes in sequence and returns the first value of that sequence.
function first(input){
if (typeof input === "string") return input.charAt(0)
return input[0]
}
assert(first("ubuntu"), "u");
assert(first([1, 2, 3]), 1);
assert(first(["JS", "is", "awesome"]), "JS");
addToDone("Exercise 50 is correct.")
// Exercise 51
// Write a function definition named second that takes in sequence and returns the second value of that sequence.
function second(input){
if (typeof input === "string") return input.charAt(1)
return input[1]
}
assert(second("ubuntu"), "b");
assert(second([1, 2, 3]), 2);
assert(second(["JS", "is", "awesome"]), "is");
addToDone("Exercise 51 is correct.")
// Exercise 52
// Write a function definition named third that takes in sequence and returns the third value of that sequence.
function third(input){
if (typeof input === "string") return input.charAt(2)
return input[2]
}
assert(third("ubuntu"), "u");
assert(third([1, 2, 3]), 3);
assert(third(["JS", "is", "awesome"]), "awesome");
addToDone("Exercise 52 is correct.")
// Exercise 53
// Write a function definition named forth that takes in sequence and returns the forth value of that sequence.
function forth(input){
if (typeof input === "string") return input.charAt(3)
return input[3]
}
assert(forth("ubuntu"), "n");
assert(forth([1, 2, 3, 4]), 4);
assert(forth(["JS", "is", "awesome", "right?"]), "right?");
addToDone("Exercise 53 is correct.")
// Exercise 54
// Write a function definition named last that takes in sequence and returns the last value of that sequence.
function last(input){
if (typeof input === "string") return input.charAt(input.length-1)
return input[input.length-1]
}
assert(last("ubuntu"), "u");
assert(last([1, 2, 3, 4]), 4);
assert(last(["JS", "is", "awesome"]), "awesome");
assert(last(["kiwi", "mango", "guava"]), "guava");
addToDone("Exercise 54 is correct.")
// Exercise 55
// Write a function definition named secondToLast that takes in sequence and returns the second to last value of that sequence.
function secondToLast(input){
if (typeof input === "string") return input.charAt(input.length-2)
return input[input.length-2]
}
assert(secondToLast("ubuntu"), "t");
assert(secondToLast([1, 2, 3, 4]), 3);
assert(secondToLast(["JS", "is", "awesome"]), "is");
assert(secondToLast(["kiwi", "mango", "guava"]), "mango");
addToDone("Exercise 55 is correct.")
// Exercise 56
// Write a function definition named thirdToLast that takes in sequence and returns the third to last value of that sequence.
function thirdToLast(input){
if (typeof input === "string") return input.charAt(input.length-3)
return input[input.length-3]
}
assert(thirdToLast("ubuntu"), "n");
assert(thirdToLast([1, 2, 3, 4]), 2);
assert(thirdToLast(["JS", "is", "awesome"]), "JS");
assert(thirdToLast(["strawberry", "kiwi", "mango", "guava"]), "kiwi");
addToDone("Exercise 56 is correct.")
// Exercise 57
// Write a function definition named firstAndSecond that takes in sequence and returns the first and second value of that sequence as an array
function firstAndSecond(input){
var returnArray = []
returnArray.push(first(input))
returnArray.push(second(input))
return returnArray
}
assert(firstAndSecond([1, 2, 3, 4]), [1, 2]);
assert(firstAndSecond(["JS", "is", "awesome"]), ["JS", "is"]);
assert(firstAndSecond(["strawberry", "kiwi", "mango", "guava"]), ["strawberry", "kiwi"]);
addToDone("Exercise 57 is correct.")
// Exercise 58
// Write a function definition named firstAndLast that takes in sequence and returns the first and last value of that sequence as an array
function firstAndLast(input){
var returnArray = []
returnArray.push(first(input))
returnArray.push(last(input))
return returnArray
}
assert(firstAndLast([1, 2, 3, 4]), [1, 4]);
assert(firstAndLast(["JS", "is", "awesome"]), ["JS", "awesome"]);
assert(firstAndLast(["strawberry", "kiwi", "mango", "guava"]), ["strawberry", "guava"]);
addToDone("Exercise 58 is correct.")
// Exercise 59
// Write a function definition named firstToLast that takes in sequence and returns the sequence with the first value moved to the end of the sequence.
function firstToLast(input){
var firstItem = input.shift()
input.push(firstItem)
return input
}
assert(firstToLast([1, 2, 3, 4]), [2, 3, 4, 1]);
assert(firstToLast(["JS", "is", "awesome"]), ["is", "awesome", "JS"]);
assert(firstToLast(["strawberry", "kiwi", "mango", "guava"]), ["kiwi", "mango", "guava", "strawberry"]);
addToDone("Exercise 59 is correct.")
// Exercise 60
// Write a function definition named sumAll that takes in sequence of numbers and returns all the numbers added together.
function sumAll(input){
var sum = 0;
for (var i = 0; i < input.length; i++){
sum+= input[i]
}
return sum
}
assert(sumAll([1, 2, 3, 4]), 10);
assert(sumAll([3, 3, 3]), 9);
assert(sumAll([0, 5, 6]), 11);
addToDone("Exercise 60 is correct.")
// Exercise 61
// Write a function definition named mean that takes in sequence of numbers and returns the average value
function mean(numbers) {
return sumAll(numbers) / numbers.length;
}
assert(mean([1, 2, 3, 4]), 2.5);
assert(mean([3, 3, 3]), 3);
assert(mean([1, 5, 6]), 4);
addToDone("Exercise 61 is correct.")
// Exercise 62
// Write a function definition named median that takes in sequence of numbers and returns the average value
function median(numbers) {
// median of [3, 5, 4, 4, 1, 1, 2, 3] = 3
var median = 0, numsLength = numbers.length;
numbers.sort();
if (isEven(numsLength)){
// average of two middle numbers
median = (numbers[numsLength / 2 - 1] + numbers[numsLength / 2]) / 2;
} else { // is odd
// middle number only
median = numbers[(numsLength - 1) / 2];
}
return median;
}
assert(median([1, 2, 3, 4, 5]), 3.0);
assert(median([1, 2, 3]), 2.0);
assert(median([1, 5, 6]), 5.0);
assert(median([1, 2, 5, 6]), 3.5);
addToDone("Exercise 62 is correct.")
// Exercise 63
// Write a function definition named maxMinusMin that takes in an array of numbers and returns the difference of the maximum minus theminimum.
function maxMinusMin(input){
input.sort()
return subtract(input[input.length-1], input[0])
}
assert(maxMinusMin([1, 2, 2, 8, 3, 4]), 7);
assert(maxMinusMin([1, 1, 2, 3, 9]), 8);
assert(maxMinusMin([2, 2, 3, 3, 3, 7]), 5);
addToDone("Exercise 63 is correct.")
// Exercise 64
// Write a function definition named productOfAll that takes in sequence of numbers and returns the product of multiplying all the numbers together
function productOfAll(input){
var temp = 1
for(let i = 0; i<input.length; i++){
temp *= input[i]
}
return temp
}
assert(productOfAll([1, 2, 3]), 6);
assert(productOfAll([3, 4, 5]), 60);
assert(productOfAll([2, 2, 3, 0]), 0);
addToDone("Exercise 64 is correct.")
// Exercise 65
// Write a function definition named getHighestNumber that takes in sequence of numbers and returns the largest number.
function getHighestNumber(input){
input.sort()
return input[input.length-1]
}
assert(getHighestNumber([1, 2, 3]), 3);
assert(getHighestNumber([1, 5, 2, 3, 4]), 5);
assert(getHighestNumber([5, 1, 2, 4, 9]), 9);
addToDone("Exercise 65 is correct.")
// Exercise 66
// Write a function definition named getSmallestNumber that takes in sequence of numbers and returns the smallest number.
function getSmallestNumber(input){
input.sort()
return input[0]
}
assert(getSmallestNumber([1, 2, 3]), 1);
assert(getSmallestNumber([3, 5, 9, 8, 1]), 1);
assert(getSmallestNumber([8, 9, 4, 5, 7]), 4);
addToDone("Exercise 66 is correct.")
// Exercise 67