Repository navigation
Expand file tree
/
Copy pathFCC JS Basic algorithm scripting.js
More file actions
381 lines (310 loc) · 12.8 KB
/
Copy pathFCC JS Basic algorithm scripting.js
File metadata and controls
381 lines (310 loc) · 12.8 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
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
/*
Basic Algorithm Scripting: Return Largest Numbers in Arrays
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
*/
function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].reduce((biggest, current) => {
if (current > biggest) {
return current;
} else {
return biggest;
}
});
}
// You can do this!
return arr;
}
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
]);
/*
Basic Algorithm Scripting: Finders Keepers
Create a function that looks through an array (first argument) and returns the first element in the array
that passes a truth test (second argument). If no element passes the test, return undefined.
*/
function findElement(arr, func) {
for (let i = 0; i < arr.length; i++) {
if (func(arr[i])) {
return arr[i];
}
}
return undefined;
}
findElement([1, 2, 3, 4], num => num % 2 === 0);
/*
Basic Algorithm Scripting: Title Case a Sentence
Return the provided string with the first letter of each word capitalized.
Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".
*/
function titleCase(str) {
let arrOfWords = str.toLowerCase().split(" ");
for (let i = 0; i < arrOfWords.length; i++) {
arrOfWords[i] = arrOfWords[i][0].toUpperCase() + arrOfWords[i].slice(1);
}
return arrOfWords.join(" ");
}
console.log(titleCase("I'm a little tea pot"));
/*
Basic Algorithm Scripting: Slice and Splice
You are given two arrays and an index.
Use the array methods slice and splice to copy each element of the first array into the second array, in order.
Begin inserting elements at index n of the second array.
Return the resulting array. The input arrays should remain the same after the function runs.
*/
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let combinedArr = [...arr2];
for (let i = 0; i < arr1.length; i++) {
combinedArr.splice(n + i, 0, arr1[i]);
}
return combinedArr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
/*
Basic Algorithm Scripting: Where do I Belong
Return the lowest index at which a value (second argument) should be inserted into an array
(first argument) once it has been sorted. The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0),
but less than 2 (index 1).
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted
it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
*/
function getIndexToIns(paramArr, num) {
// Find my place in this sorted array.
let arr = [...paramArr];
arr.push(num);
// simple bubble sort
for (let j = 0; j < arr.length; j++) {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
return arr.findIndex(el => el == num);
}
getIndexToIns([40, 60], 50);
/*
Basic Algorithm Scripting: Mutations
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
*/
function mutation(arr) {
let source = arr[0].toLowerCase();
let condition = arr[1].toLowerCase().split("");
for (let i = 0; i < condition.length; i++) {
if (source.indexOf(condition[i]) == -1) {
return false;
}
}
return true;
}
console.log(mutation(["helLo", "he"]));
/*
Basic Algorithm Scripting: Chunky Monkey
Write a function that splits an array (first argument) into groups the length of size (second argument)
and returns them as a two-dimensional array.
*/
function chunkArrayInGroups(arr, size) {
// Break it up.
let resultArr = [];
for (let i = 0; i < arr.length; i += size) {
let tempArr = [];
for (let j = 0; j < size && i + j < arr.length; j++) {
tempArr.push(arr[i + j]);
}
resultArr.push(tempArr);
}
return resultArr;
}
console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
/*
Functional Programming: Use the reduce Method to Analyze Data
Array.prototype.reduce(), or simply reduce(), is the most general of all array operations in JavaScript.
You can solve almost any array processing problem using the reduce method.
This is not the case with the filter and map methods since they do not allow interaction between
two different elements of the array. For example, if you want to compare elements of the array,
or add them together, filter or map could not process that.
The reduce method allows for more general forms of array processing, and it's possible to show that both
filter and map can be derived as a special application of reduce.
However, before we get there, let's practice using reduce first.
The variable watchList holds an array of objects with information on several movies. Use reduce to find the average IMDB rating of the movies directed by Christopher Nolan. Recall from prior challenges how to filter data and map over it to pull what you need. You may need to create other variables, but save the final average into the variable averageRating. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations.
*/
// the global variable
var watchList = [
{
Title: "Inception",
Year: "2010",
Rated: "PG-13",
Released: "16 Jul 2010",
Runtime: "148 min",
Genre: "Action, Adventure, Crime",
Director: "Christopher Nolan",
Writer: "Christopher Nolan",
Actors: "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
Plot:
"A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
Language: "English, Japanese, French",
Country: "USA, UK",
Awards: "Won 4 Oscars. Another 143 wins & 198 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
Metascore: "74",
imdbRating: "8.8",
imdbVotes: "1,446,708",
imdbID: "tt1375666",
Type: "movie",
Response: "True"
},
{
Title: "Interstellar",
Year: "2014",
Rated: "PG-13",
Released: "07 Nov 2014",
Runtime: "169 min",
Genre: "Adventure, Drama, Sci-Fi",
Director: "Christopher Nolan",
Writer: "Jonathan Nolan, Christopher Nolan",
Actors: "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
Plot:
"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
Language: "English",
Country: "USA, UK",
Awards: "Won 1 Oscar. Another 39 wins & 132 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
Metascore: "74",
imdbRating: "8.6",
imdbVotes: "910,366",
imdbID: "tt0816692",
Type: "movie",
Response: "True"
},
{
Title: "The Dark Knight",
Year: "2008",
Rated: "PG-13",
Released: "18 Jul 2008",
Runtime: "152 min",
Genre: "Action, Adventure, Crime",
Director: "Christopher Nolan",
Writer:
"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
Actors: "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
Plot:
"When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
Language: "English, Mandarin",
Country: "USA, UK",
Awards: "Won 2 Oscars. Another 146 wins & 142 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
Metascore: "82",
imdbRating: "9.0",
imdbVotes: "1,652,832",
imdbID: "tt0468569",
Type: "movie",
Response: "True"
},
{
Title: "Batman Begins",
Year: "2005",
Rated: "PG-13",
Released: "15 Jun 2005",
Runtime: "140 min",
Genre: "Action, Adventure",
Director: "Christopher Nolan",
Writer:
"Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
Actors: "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
Plot:
"After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
Language: "English, Urdu, Mandarin",
Country: "USA, UK",
Awards: "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
Metascore: "70",
imdbRating: "8.3",
imdbVotes: "972,584",
imdbID: "tt0372784",
Type: "movie",
Response: "True"
},
{
Title: "Avatar",
Year: "2009",
Rated: "PG-13",
Released: "18 Dec 2009",
Runtime: "162 min",
Genre: "Action, Adventure, Fantasy",
Director: "James Cameron",
Writer: "James Cameron",
Actors: "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
Plot:
"A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
Language: "English, Spanish",
Country: "USA, UK",
Awards: "Won 3 Oscars. Another 80 wins & 121 nominations.",
Poster:
"http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
Metascore: "83",
imdbRating: "7.9",
imdbVotes: "876,575",
imdbID: "tt0499549",
Type: "movie",
Response: "True"
}
];
// Add your code below this line
let filteredArr = watchList.filter(
item => item.Director == "Christopher Nolan"
);
let ratingsArr = filteredArr.map(movie => parseFloat(movie.imdbRating));
let averageRating =
ratingsArr.reduce((acc, cur) => (acc += cur)) / ratingsArr.length;
// Add your code above this line
console.log(averageRating);
/*
// Use the sort method in the nonMutatingSort function to sort the elements of an array in ascending order.
// The function should return a new array, and not mutate the globalArray variable.
*/
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
return arr.concat([]).sort((a, b) => a - b);
// Add your code above this line
}
nonMutatingSort(globalArray);
/*
Functional Programming: Apply Functional Programming to Convert Strings to URL Slugs
The last several challenges covered a number of useful array and string methods that follow functional programming principles. We've also learned about reduce, which is a powerful method used to reduce problems to simpler forms. From computing averages to sorting, any array operation can be achieved by applying it. Recall that map and filter are special cases of reduce.
Let's combine what we've learned to solve a practical problem.
Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled "Stop Using Reduce", it's likely the URL would have some form of the title string in it (".../stop-using-reduce"). You may have already noticed this on the freeCodeCamp site.
Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace. Here are the requirements:
The input is a string with spaces and title-cased words
The output is a string with the spaces between words replaced by a hyphen (-)
The output should be all lower-cased letters
The output should not have any spaces
*/
// the global variable
var globalTitle = "Winter Is Coming";
// Add your code below this line
function urlSlug(title) {
return title
.toLowerCase()
.split(/\s+/)
.filter(item => item != "")
.join("-");
}
// Add your code above this line
var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"