-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathsolutions.js
More file actions
348 lines (263 loc) · 9.81 KB
/
solutions.js
File metadata and controls
348 lines (263 loc) · 9.81 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
/* 1) Ticket Generator
Write a for-loop that will iterate through 20 numbers (starting at 1 and ending at 20) and console.log the following message:
"Now serving 1."
"Now serving 2."
"Now serving 3."
.
.
.
"Now serving 20."
*/
for(var i = 1; i<21; i++){
console.log("Now serving " + i);
}
/* 2) Pop Charts
Write a for-loop that will iterate through the topFive array below and console.log the following message:
This week's chart buster is: 'Closer.'
This week's chart buster is: 'Starboy.'
This week's chart buster is: 'I Feel It Coming.'
This week's chart buster is: 'Let Me Love You.'
This week's chart buster is: '24K Magic.'
*/
var topFive = ["Closer", "Starboy", "I Feel It Coming", "Let Me Love You", "24K Magic"];
for(var i = 0; i<topFive.length; i++){
console.log("This week's chart buster is: " + "'"+ topFive[i] +".'");
}
/* 3) Dead Presidents
Declare a variable named `presidents` and assign it to an array containing the following Presidents: Washington, Adams, Jefferson, Madison and Monroe.
Write a for-loop that will iterate through this array and within the for-loop console.log the following:
The value at 0 is Washington.
The value at 1 is Adams.
The value at 2 is "Jefferson.
The value at 3 is "Madison.
The value at 4 is Monroe.
Next, console.log the length of the array.
3-A) Create a function named leaders which will take in a parameter: `person`.
@param Datatype: String `person`
This function will iterate through the person parameter and console.log the following message for each item in the array:
"President person was a great leader."
*/
var presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"];
for(var i = 0; i<presidents.length; i++){
console.log("The value at " + i + " is " + presidents[i]);
}
console.log(presidents.length);
function leaders(person){
for(var i = 0; i<person.length; i++){
console.log("President " + person[i] + " was a great leader.");
}
}
leaders(presidents);
/* 4) Line Number
Declare a variable named `stringOfNumbers` and assign its value to an empty string.
Write a for-loop that concatenates a Number value into that string on each iteration, starting at `10` and continuing up to and including `20`. Console.log your result. It should read "1011121314151617181920"*/
var stringOfNumbers = "";
for(var i = 10; i<21; i++){
stringOfNumbers += i;
}
console.log(stringOfNumbers);
/* 5) Even Stevens
Declare a variable named `evenNumArr` and assign its value to an empty array.
Write a for-loop that will push even numbers to the `evenNumArr` array. We want to push 50 even even numbers starting from 0.
Console.log your results.
*/
var evenNumArr = [];
for(var i = 0; i<100; i++){
if(i%2 === 0){
evenNumArr.push(i);
}
}
console.log(evenNumArr);
/* 6) Up the Odds
Declare a variable named `oddSum` and assign it to the Number value 0.
Write a for-loop that will sum up odd numbers to the `oddSum` variable. We want to add 50 odd numbers starting from 1.
Console.log your results.
*/
var oddSum = 0;
for(var i = 1; i<100; i++){
if(i%2 === 1){
oddSum +=i;
}
}
console.log(oddSum);
/* 7) Oops There It is
Declare a variable named `oopsArray` and assign its to the following array: `[ 'turn' , , 'down' , , 'for' , , 'what' ]`.
Note that every odd index value in `oopsArray` is currently `undefined`. Using a for-loop, add the string `'nope'` to every odd index. Console.log your result. It should look like this:
[ 'turn' , 'nope' , 'down' , 'nope' , 'for' , 'nope' , 'what' ]
*/
var oopsArray = [ 'turn' , , 'down' , , 'for' , , 'what' ];
for(var i = 0; i<oopsArray.length; i++){
console.log(oopsArray[i]);
if(i%2 ===1){
oopsArray[i] = "nope";
}
}
console.log(oopsArray);
/* 8) Is It There Oops
Using a for-loop, iterate through the Array stored at `oopsArray` backwards. Console.log your result. It should look like this:
what
nope
for
nope
down
nope
turn
*/
for(var i = oopsArray.length - 1; i>=0; i--){
console.log(oopsArray[i]);
}
/* 9) Siesta Time
Declare a variable named `napSchedule` and assign its value to the following array: `[false, false, true, false, true, true]`
Next, write a function named `nap`. This function takes in a single parameter: `schedule`
@param Datatype: Array `schedule`
Inside of this function write a for-loop that will iterate through the `napSchedule` array and console.log the message: `ZzZzZzZz` if the schedule is `true`, otherwise the it will console.log the message: `Gotta get coding!` if the schedule is `false`.
*/
var napSchedule = [false, false, true, false, true, true];
function nap(schedule){
for(var i = 0; i<schedule.length; i++){
if(schedule[i] === true){
console.log("ZzZzZzZz");
}else{
console.log("Gotta get coding!");
}
}
}
nap(napSchedule);
/* 10) Copy Pasta
Declare a variable named `valuesArray` and assign its value to be an array: `[99, 66, 829, 1941, 8, 76]`.
Declare another variable named `copyValuesArray` and assing its value to an empty array.
Write a function named `copyArray` which takes two arguments: `originArray` and `destinationArray`.
@param Datatype: Array `originArray`
@param Datatype: Array `destinationArray`
Inside of this function write a for-loop that will iterate through the contents of the `originArray` and pushes each element of that array into `destinationArray`. Console.log your result.
*/
var valuesArray = [99, 66, 829, 1941, 8, 76];
var copyValuesArray = [];
function copyArray(originArray, destinationArray){
for(var i = 0; i<originArray.length; i++){
destinationArray.push(originArray[i]);
}
return destinationArray;
}
console.log(copyArray(valuesArray, copyValuesArray));
console.log(copyValuesArray);
/*Final Boss*/
/* 11) Go Long
Declare a variable named `topQuote` and assign it to a String value of your favorite one line quote.
Write a function that will iterate through the string value and return the longest word in that quote. Console.log your result.
*/
var topQuote = "Boards don't hit back.";
function longestWord(str){
var strToArray = str.split(" "); //converts string to an array
var currentWord = ""; //to store the string
for(var i =0; i<strToArray.length; i++){ //iterate through the string array
if(strToArray[i].length > currentWord.length){ //comparing if the word's length is longer than current word
currentWord = strToArray[i];
}
}
return currentWord;
}
console.log(longestWord(topQuote));
/* 12) Puppet Master
Declare a variable named `miscStorage` set it's value to be: `[ [], 'Carrots', 9, 'Beets', {}, {name: "Todd B."}, 'Mush' ]`
Write a function named `generateArrayOfStrings` which takes a single argument `storage`. This function will return a new Array with only `String` values inside of it.
@param Datatype: Array `storage`
@return Datatype: Array
*/
var miscStorage = [ [], 'Carrots', 9, 'Beets', {}, {name: "Todd B."}, 'Mush' ];
function generateArrayOfStrings(storage){
var stringArray = [];
for(var i = 0; i<storage.length; i++){
if(typeof storage[i] === typeof ""){ //typeof operator returns a string https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
stringArray.push(storage[i]);
}
}
return stringArray;
}
console.log(generateArrayOfStrings(miscStorage));
/* 13) All Grown Up
Write a function that will capitalize the first letter in each word in the phrase below. The function will console.log the message: "I've Lived A Life That's Full. I've Traveled Each and Every Highway. But More, Much More Than This. I Did It My Way."
*/
var myWay = "i've lived a life that's full, i've traveled each and every highway. but more, much more than this. i did it my way.";
function capitalize(str){
var word = str.split(" ");
//console.log(word);
for(var i = 0; i<word.length; i++){
//console.log(word[i]);
var letter = word[i].split("");
//console.log(letter);
letter[0] = letter[0].toUpperCase();
//console.log(letter);
word[i] = letter.join('');
}
return word.join(" ");
}
console.log(capitalize(myWay));
/* 14) Sightseeing
Write a function that will loop through the multi-dimensional array and console.log the city and landmark on separate lines.
Note, please use two for loops to complete this problem
@param Datatype: Array
*/
var guide = [["Honolulu", "Waikiki"], ["Tokyo", "Tsukiji Fish Market"], ["Cairo", "Pyramids"],["Rome", "Colosseum"]];
for(var i = 0; i<guide.length; i++){
for(var j = 0; j<guide[i].length; j++){
console.log(guide[i][j])
}
}
/* 15) Back to School
Declare a variable named `currentCohort` and set it's value to be this [array found here](https://gist.github.com/sgnl/e40879b2249e06ca7811).
Write a function named `graduateAndSetNewClass`, which takes a single argument called `cohort`.
@param Datatype: Array `cohort`
The function will iterate through the `cohort` argument and check each student's `enrolled` property.
If the `enrolled` property is set to `true` then change that student's `graduated` property to `true`. Otherwise, if `enrolled` is set to `false` then change `enrolled` to `true` leaving `graduated` alone and unchanged.
Console.log your result.
*/
var currentCohort = [
{
name: 'Doug',
graduated: false,
enrolled: true
},
{
name: 'Pat',
graduated: false,
enrolled: false
},
{
name: 'Marsha',
graduated: false,
enrolled: false
},
{
name: 'Moira',
graduated: false,
enrolled: true
},
{
name: 'Ben',
graduated: false,
enrolled: true
},
{
name: 'Nigel the Giraffe',
graduated: false,
enrolled: false
},
{
name: 'Brandon the Shark',
graduated: false,
enrolled: true
}
];
function graduateAndSetNewClass(cohort){
for(var i = 0; i<cohort.length; i++){
//console.log(cohort[i]);
if(cohort[i].enrolled === true){
cohort[i].graduated = true;
}else{
cohort[i].graduated = false;
}
}
return cohort;
}
console.log(graduateAndSetNewClass(currentCohort));