Skip to content

Commit d1c890f

Browse files
committed
#100DaysOfCode - Day 5
Practice functions of array
1 parent 5f48243 commit d1c890f

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

04 - Array Cardio Day 1/array.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var fruits = ["Apple", "Banana"];
2+
3+
// Loop over an Array
4+
fruits.forEach(function (item, index, array) {
5+
console.log(item, index);
6+
});
7+
// Apple 0
8+
// Banana 1
9+
10+
// Add to the end of an Array
11+
var newLength = fruits.push("Orange");
12+
// ["Apple", "Banana", "Orange"]
13+
14+
// Remove from the end of an Array
15+
var last = fruits.pop(); // remove Orange (from the end)
16+
// ["Apple", "Banana"];
17+
18+
// Remove from the front of an Array
19+
var first = fruits.shift(); // remove Apple from the front
20+
// ["Banana"];
21+
22+
// Add to the front of an Array
23+
var newLength = fruits.unshift("Strawberry") // add to the front
24+
// ["Strawberry", "Banana"];
25+
26+
// Find the index of an item in the Array
27+
fruits.push("Mango");
28+
// ["Strawberry", "Banana", "Mango"]
29+
var pos = fruits.indexOf("Banana");
30+
// 1
31+
32+
// Remove an item by Index Position
33+
var removedItem = fruits.splice(pos, 1); // this is how to remove an item,
34+
// ["Strawberry", "Mango"]
35+
36+
// Remove items from an Index Position
37+
var removedItems = fruits.splice(pos, n); // this is how to remove items, n defines the number of items to be removed,
38+
// from that position onward to the end of array.
39+
// let, n = 1;
40+
// ["Strawberry"]
41+
42+
// Copy an Array
43+
var shallowCopy = fruits.slice(); // this is how to make a copy
44+
// ["Strawberry"]

04 - Array Cardio Day 1/index-START.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,31 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
var filteredArray = inventors.filter(function(value) {
35+
return value.year >= 1500 && value.year < 1600;
36+
});
37+
console.log(filteredArray);
3438

3539
// Array.prototype.map()
3640
// 2. Give us an array of the inventors' first and last names
41+
var mappedArray = inventors.map(function(value) {
42+
return {"first": value.first, "last": value.last};
43+
});
44+
console.log(mappedArray);
3745

3846
// Array.prototype.sort()
3947
// 3. Sort the inventors by birthdate, oldest to youngest
48+
var sortedArray = inventors.sort(function(val1, val2) {
49+
return val1.year - val2.year;
50+
});
51+
console.log(sortedArray);
4052

4153
// Array.prototype.reduce()
4254
// 4. How many years did all the inventors live?
55+
const totalYears = inventors.reduce((total, inventor) => {
56+
return total + (inventor.passed - inventor.year);
57+
});
58+
console.log(totalYears);
4359

4460
// 5. Sort the inventors by years lived
4561

0 commit comments

Comments
 (0)