-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek3-Coding-Project.js
More file actions
225 lines (195 loc) · 6.56 KB
/
Week3-Coding-Project.js
File metadata and controls
225 lines (195 loc) · 6.56 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
console.log('\tQ: 1');
let ages = [3, 9, 23, 64, 2, 8, 28, 93]; //8
console.log(ages);
//1.a. Programmatically subtract the value of the first element in the array
//from the value in the last element of the array
console.log('\tQ: 1a');
console.log(ages.slice(-1) - ages[0]); //90 trying the slice method
console.log(ages[ages.length-1] - ages[0]) //90
//1.b. Add a new age to your array and repeat the step above to ensure it is dynamic.
//(works for arrays of different lengths).
console.log('\tQ: 1b');
ages.push(33);
console.log(ages);
console.log(ages.slice(-1) - ages[0]); //30
console.log(ages[ages.length-1] - ages[0]); //30
//1.c. Use a loop to iterate through the array
//and calculate the average age.
console.log('\tQ: 1c');
function calculateAverage(array) {
var total = 0;
var count = 0;
array.forEach(function(ages) {
total += ages;
count++;
});
return total / count;
}
console.log(calculateAverage(ages)); //29.22 trying forEach loop
function testArray(ageNum) {
let sum4 = 0
for(let i = 0; i < ageNum.length; i++) {
sum4 += ageNum[i]
}
let ageAverage = sum4 / ageNum.length
return ageAverage
}
console.log(testArray(ages));
//2. Create an array called names that contains the following values:
//‘Sam’, ‘Tommy’, ‘Tim’, ‘Sally’, ‘Buck’, ‘Bob’
console.log('\tQ: 2');
let names = ['Sam', 'Tommy', 'Tim', 'Sally', 'Buck', 'Bob'];
console.log(names);
//2.a. Use a loop to iterate through the array and calculate the
//average number of letters per name.
console.log('\tQ: 2a');
let sum = 0
for (let i = 0; i < names.length; i++) {
sum = sum + names[i].length
}
let namesAverage = sum / names.length
console.log(namesAverage); //3.83
//2.b. Use a loop to iterate through the array again and
//concatenate all the names together, separated by spaces.
console.log('\tQ: 2b');
let myString = names.join(' ');
console.log(myString);
let concat = '';
for (let i = 0; i < names.length; i++) { //same result using a for loop
//console.log(names[i]);
concat = concat + names[i] + " ";
}
console.log(concat);
//3. How do you access the last element of any array?
console.log("\tQ: 3");
console.log(names[names.length-1]);
//4. How do you access the first element of any array?
console.log('\tQ: 4');
console.log(names[0]);
//5. Create a new array called nameLengths.
//Write a loop to iterate over the previously created names
//array and add the length of each name to the nameLengths array.
console.log('\tQ: 5');
namesArray = ['Kelly', 'Sam', 'Kate'];
let nameLengths = namesArray.map(function(firstName) {
return firstName.length;
})
console.log(nameLengths); // trying map method
function createNumberArray(namesArray) {
let arrayLength = [];
for(let i = 0; i < namesArray.length; i++) {
arrayLength.push(namesArray[i].length);
}
return arrayLength;
}
nameLengths = createNumberArray(namesArray);
console.log(nameLengths);
//6. Write a loop to iterate over the nameLengths array and calculate
//the sum of all the elements in the array.
console.log('\tQ: 6');
let total = 0
for (let i = 0; i < nameLengths.length; i++) {
//console.log(nameLengths[i])
total = total + nameLengths[i]
}
console.log(total); //12
//7. Write a function that takes two parameters, word and n,
//as arguments and returns the word concatenated to itself n number of times.
//(i.e. if I pass in ‘Hello’ and 3, I would expect the function to return ‘HelloHelloHello’).
console.log('\tQ: 7');
function repeatWord(word, n) {
console.log(word.repeat(n))
}
repeatWord('Hello',3)
//8. Write a function that takes two parameters,
//firstName and lastName, and returns a full name.
console.log('\tQ: 8');
function createFullName(firstName, lastName) {
return firstName + " " + lastName
}
console.log(createFullName('Kate', 'Running'));
//9. Write a function that takes an array of numbers and
//returns true if the sum of all the numbers in the array is greater than 100.
console.log('\tQ: 9');
let arrayNum = [50, 50, 20, 30]
function totalNums(arrayNum) {
let sums = 0;
for(let i = 0; i < arrayNum.length; i++) {
sums += arrayNum[i];
console.log(sums)
}
if (sums > 100) {
return true
}
if (sums <= 100) {
return false
}
}
console.log(totalNums(arrayNum));
//10. Write a function that takes an array of numbers and returns the average of all the elements in the array.
console.log('\tQ: 10');
let number1 = [10, 20, 30, 40]
function numberArray (num1) {
let newSum = 0;
for(let i = 0; i < num1.length; i++) {
newSum += num1[i];
}
let arrayAverage = newSum / num1.length
return arrayAverage
}
console.log(numberArray(number1));
//11. Write a function that takes two arrays of numbers and returns true if the average of the elements in the
//first array is greater than the average of the elements in the second array.
console.log('\tQ: 11');
let array1 = [20, 10]
let array2 = [6, 8]
function twoArrays (a1, a2) {
let sum1 = 0;
let sum2 = 0;
for(let i = 0; i < a1.length; i++) {
sum1 += a1[i]
}
let average1 = sum1 / a1.length
for(let i = 0; i < a2.length; i++) {
sum2 += a2[i]
}
let average2 = sum2 / a2.length
console.log(average1);
console.log(average2);
if (average1 > average2) {
return true
} else {
return false
}
}
console.log(twoArrays(array1, array2))
//12. Write a function called willBuyDrink that takes a boolean isHotOutside,
//and a number moneyInPocket, and returns true if it is hot outside and if moneyInPocket is greater than 10.50.
console.log('\tQ: 12');
let isHotOutside = true
let moneyInPocket = 15
function willBuyDrink (temperature, number) {
if (temperature === true && number > 10.50) {
console.log("can I buy a drink")
return true
} else {
console.log("no you cannot!")
return false
}
console.log("money", number > 10.50)
console.log("test", temperature === true)
console.log("temperature:", temperature, "number:", number)
}
let drink = willBuyDrink(isHotOutside, moneyInPocket) //isHotOutside = temperature, moneyInPocket = number
console.log(drink);
//13. Create a function of your own that solves a problem.
//In comments, write what the function does and why you created it.
console.log('\tQ: 13');
const numberArray5 = [20, 600, 5, 35, 75];
function compareNumbers(a, b) { //function is checking the value of the elements
return a - b;
}
numberArray5.sort();
console.log(numberArray5);
numberArray5.sort(compareNumbers); // now the array is sorted in numeric value
console.log(numberArray5);