-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek3CodingHW.js
189 lines (155 loc) · 4.25 KB
/
Week3CodingHW.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
//********
// Q#1a
//********
var ages = [3, 9, 23, 64, 2, 8, 28, 93];
var difference = ages[ages.length - ages.length] - ages[ages.length - 1];
console.log("The difference between the first and last element within the array is " + difference);
//********
// Q#1b
//********
ages.splice(8, 0, 57);
var newAge = ages;
var newDifference = newAge[newAge.length - newAge.length] - newAge[newAge.length - 1];
console.log("The difference between the first and last element within the array is " + newDifference);
//********
// Q#1c
//********
var count = 0;
for (var i=0; i<newAge.length; i++) {
count += newAge[i];
}
var average = (count/newAge.length);
console.log("The average age within this array is " + average);
//********
// Q#2
//********
var arrayStrings = ['Sam', 'Tommy', 'Tim', 'Sally', 'Buck', 'Bob'];
var count = 0;
for (var i=0; i<arrayStrings.length; i++) {
count += arrayStrings[i].length;
}
var average = (count/arrayStrings.length);
console.log("The average of the strings of the name in the array is " + average.toFixed(2));
var allStrings = " ";
for (var i=0; i<arrayStrings.length; i++) {
allStrings += (arrayStrings[i] + " ");
}
console.log("The list of names in the array are as follows:"+ allStrings);
//********
// Q#3
//********
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var lastElement = array[array.length - 1];
console.log("The last element in the array is " + lastElement);
//********
// Q#4
//********
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var firstElement = array[0];
console.log("The first element in the array is " + firstElement);
//********
// Q#5
//********
var arrayStrings = ['Sam', 'Tommy', 'Tim', 'Sally', 'Buck', 'Bob'];
var nameLengths = [];
for (var i=0; i<arrayStrings.length; i++) {
nameLengths[i]=arrayStrings[i].length;
}
console.log(nameLengths);
//********
// Q#6
//********
var count = 0;
for (var i=0; i<nameLengths.length; i++) {
count += nameLengths[i];
}
var average = (count/nameLengths.length);
console.log("The average of the namelength in the array is " + average.toFixed(2));
//********
// Q#7
//********
var string = "Hello";
var count = 3;
console.log(string.repeat(count));
//********
// Q#8
//********
var firstName = 'Caron';
var lastName = 'Ramos';
var fullName = firstName.concat(" ", lastName);
console.log(fullName);
//********
// Q#9
//********
var arrayOfIntegers = [5, 10];
var sum = arrayOfIntegers.reduce(function (a, b){
if((a + b) > 100){
return true + ", " + (a + b) + " is greater than 100.";
}
else{
return false + ", " + (a + b) + " is less than 100.";
}
});
console.log(sum);
//********
// Q#10
//********
var arrayDoubles = [1.0, 2.13, 3.4, 4.56, 5.7, 6.89, 7.10, 8.11, 9.012, 10.13];
var count = 0;
for (var i=0; i<arrayDoubles.length; i++) {
count += arrayDoubles[i];
}
var average = (count/arrayDoubles.length);
console.log("The average of the doubles in the array is " + average);
//********
// Q#11
//********
var arrayDoubles = [1.0, 2.13, 3.4, 4.56, 5.7, 6.89, 7.10, 8.11, 9.012, 10.13];
var count = 0;
for (var i=0; i<arrayDoubles.length; i++) {
count += arrayDoubles[i];
}
var average = (count/arrayDoubles.length);
console.log("The average of the doubles in the array is " + average);
var arrayDoubles2 = [1.1, 2.3, 3.45, 4.6, 5.78, 6.9, 7.1011, 8.012, 9.1314, 10.015];
var count2 = 0;
for (var i=0; i<arrayDoubles2.length; i++) {
count2 += arrayDoubles2[i];
}
var average2 = (count2/arrayDoubles2.length);
console.log("The average of the doubles in the array is " + average2);
function greaterThan(){
if (average > average2){
return true;
}
else{
return false;
}
}
console.log(greaterThan());
//********
// Q#12
//********
let isHotOutside = true;
var moneyInPocket = 10;
function willBuyDrink(){
if(isHotOutside == true && moneyInPocket>10.50){
return true;
}
else{
return false;
}
}
console.log(willBuyDrink());
//********
// Q#13
//********
const ages = [9, 15, 25, 36, 47];
const checkIfAdult = ages.filter(function(adult){
if(adult > 18){
return adult;
}
});
console.log(checkIfAdult);
//Comments: This method goes through the array of ages and checks whether each element is above 18 using the filter() function.
//The reason that this was created is that it would helped to filter out the ages that are considered adult.