Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa12a34

Browse files
committed
Array Cardio and JSON Homework
1 parent 491b365 commit aa12a34

File tree

4 files changed

+267
-3
lines changed

4 files changed

+267
-3
lines changed

Homework JSON/arrayCardio.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
const inventors = [
2+
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
3+
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
4+
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
5+
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
6+
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
7+
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
8+
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
9+
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
10+
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
11+
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
12+
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
13+
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 },
14+
];
15+
16+
const people = [
17+
'Bernhard, Sandra',
18+
'Bethea, Erin',
19+
'Becker, Carl',
20+
'Bentsen, Lloyd',
21+
'Beckett, Samuel',
22+
'Blake, William',
23+
'Berger, Ric',
24+
'Beddoes, Mick',
25+
'Beethoven, Ludwig',
26+
'Belloc, Hilaire',
27+
'Begin, Menachem',
28+
'Bellow, Saul',
29+
'Benchley, Robert',
30+
'Blair, Robert',
31+
'Benenson, Peter',
32+
'Benjamin, Walter',
33+
'Berlin, Irving',
34+
'Benn, Tony',
35+
'Benson, Leana',
36+
'Bent, Silas',
37+
'Berle, Milton',
38+
'Berry, Halle',
39+
'Biko, Steve',
40+
'Beck, Glenn',
41+
'Bergman, Ingmar',
42+
'Black, Elk',
43+
'Berio, Luciano',
44+
'Berne, Eric',
45+
'Berra, Yogi',
46+
'Berry, Wendell',
47+
'Bevan, Aneurin',
48+
'Ben-Gurion, David',
49+
'Bevel, Ken',
50+
'Biden, Joseph',
51+
'Bennington, Chester',
52+
'Bierce, Ambrose',
53+
'Billings, Josh',
54+
'Birrell, Augustine',
55+
'Blair, Tony',
56+
'Beecher, Henry',
57+
'Biondo, Frank',
58+
];
59+
const BoulevardsInParis = [
60+
'Boulevard Auguste-Blanqui',
61+
'Boulevard Barbès',
62+
'Boulevard Beaumarchais',
63+
"Boulevard de l'Amiral-Bruix",
64+
'Boulevard Mortier',
65+
'Boulevard Poniatowski',
66+
'Boulevard Soult',
67+
'Boulevard des Capucines',
68+
'Boulevard de la Chapelle',
69+
'Boulevard de Clichy',
70+
'Boulevard du Crime',
71+
"Boulevard des du Général-d'Armée-Jean-Simon",
72+
'Boulevard Haussmann',
73+
"Boulevard de l'Hôpital",
74+
'Boulevard des Italiens',
75+
'Boulevard Lefebvre',
76+
'Boulevard de Magenta',
77+
'Boulevard Marguerite-de-Rochechouart',
78+
'Boulevard Montmartre',
79+
'Boulevard du Montparnasse',
80+
'Boulevard Raspail',
81+
'Boulevard Richard-Lenoir',
82+
'Boulevard Saint-Germain',
83+
'Boulevard de Sébastopol',
84+
'Boulevard de Strasbourg',
85+
'Boulevard du Temple',
86+
'Boulevard Voltaire',
87+
'Boulevard de la Zone',
88+
];
89+
// Array.prototype.filter()
90+
// 1. Filter the list of inventors for those who were born in the 1500's
91+
const filteredInventors = inventors.filter((inventor) => {
92+
return inventor.year >= 1500 && inventor.year < 1600;
93+
});
94+
console.log(inventors);
95+
console.log(filteredInventors);
96+
// Array.prototype.map()
97+
// 2. Give us an array of the inventors first and last names
98+
const inventorsName = inventors.map((inventor) => {
99+
return `${inventor.first} ${inventor.last}`;
100+
});
101+
console.log(inventorsName);
102+
// Array.prototype.sort()
103+
// 3. Sort the inventors by birthdate, oldest to youngest
104+
105+
// const oldestToYoungest = inventors.sort((a, b) => {
106+
// if (a.year < b.year) {
107+
// return -1;
108+
// } else if (a.year > b.year) {
109+
// return 1;
110+
// } else {
111+
// return 0;
112+
// }
113+
// });
114+
115+
const oldestToYoungest = inventors.sort((a, b) => {
116+
return a.year - b.year;
117+
});
118+
console.log(oldestToYoungest);
119+
// Array.prototype.reduce()
120+
// 4. How many years did all the inventors live all together?
121+
// -------------MY Solution-----------------------
122+
// const yearsArray = inventors.map((inventor) => {
123+
// return inventor.passed - inventor.year;
124+
// });
125+
// console.log(yearsArray);
126+
// const sumYears = yearsArray.reduce((sum, year) => {
127+
// console.log(`year is:${year} and sum is: ${sum}`);
128+
// return year + sum;
129+
// }, 0);
130+
131+
const totalYears = inventors.reduce((total, inventor) => {
132+
return total + (inventor.passed - inventor.year);
133+
}, 0);
134+
console.log(totalYears);
135+
// console.log(sumYears);
136+
// 5. Sort the inventors by years lived
137+
const mostLivedYears = inventors.sort((a, b) => {
138+
return b.passed - b.year - (a.passed - a.year);
139+
});
140+
console.log(mostLivedYears);
141+
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
142+
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
143+
const de = BoulevardsInParis.filter((item) => {
144+
return item.includes('de', 0);
145+
});
146+
console.log(de);
147+
// 7. sort Exercise
148+
// Sort the people alphabetically by last name
149+
const sortByLastName = people.sort((last, next) => {
150+
const [aLast, aFirst] = last.split(', ');
151+
const [bLast, bFirst] = next.split(', ');
152+
return aLast > bLast ? 1 : -1;
153+
});
154+
console.log(sortByLastName);
155+
// 8. Reduce Exercise
156+
// Sum up the instances of each of these
157+
const data = [
158+
'car',
159+
'car',
160+
'truck',
161+
'truck',
162+
'bike',
163+
'walk',
164+
'car',
165+
'van',
166+
'bike',
167+
'walk',
168+
'car',
169+
'van',
170+
'car',
171+
'truck',
172+
];
173+
174+
const instances = data.reduce((obj, item) => {
175+
if (!obj[item]) {
176+
obj[item] = 0;
177+
}
178+
obj[item]++;
179+
return obj;
180+
}, {});
181+
console.log(instances);

Homework JSON/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<script src="./index.js"></script>
11+
<script src="./arrayCardio.js"></script>
12+
</body>
13+
</html>

Homework JSON/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
{
3+
let gudrunStore = [
4+
{
5+
"product": "Milk, 1L carton",
6+
"best_before": "2018-10-04",
7+
"price": 15.5,
8+
},
9+
{
10+
"product": "Milk, 1L carton",
11+
"best_before": "2018-10-04",
12+
"price": 15.5,
13+
},
14+
{
15+
"product": "Eggs 12pcs",
16+
"best_before": "2018-10-04",
17+
"price": 35.0,
18+
},
19+
{
20+
"product": "Swedish meatballs",
21+
"best_before": "2018-10-025",
22+
"price": 22.0,
23+
}
24+
]
25+
26+
let myJson = JSON.stringify(gudrunStore);
27+
28+
function cheap(array) {
29+
let parseJSON = JSON.parse(array);
30+
let minPrice = parseJSON.map((item) => {
31+
return item.price;
32+
})
33+
console.log(minPrice);
34+
let cheapest = Math.min(...minPrice);
35+
console.log(cheapest);
36+
let cheapItems = [];
37+
for (let i = 0; i < gudrunStore.length; i++) {
38+
if (gudrunStore[i].price === cheapest) {
39+
cheapItems.push(gudrunStore[i]);
40+
} else {
41+
console.log("not cheap");
42+
}
43+
}
44+
return JSON.stringify(cheapItems);
45+
};
46+
47+
function expensive(array) {
48+
let parseJSON = JSON.parse(array);
49+
console.log(parseJSON);
50+
let maxPrice = parseJSON.map((item) => {
51+
return item.price;
52+
})
53+
console.log(maxPrice);
54+
let expensive = Math.max(...maxPrice);
55+
console.log(expensive);
56+
let expensiveItems = [];
57+
for (let i = 0; i < gudrunStore.length; i++) {
58+
if (gudrunStore[i].price === expensive) {
59+
console.log(gudrunStore[i]);
60+
expensiveItems.push(gudrunStore[i]);
61+
} else {
62+
console.log("not expensive");
63+
}
64+
}
65+
return JSON.stringify(expensiveItems);
66+
}
67+
console.log(`cheapest items`, cheap(myJson));
68+
console.log(`most expensive items`, expensive(myJson));
69+
}

Week1/homework/app.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@
9797
bookTitle.setAttribute('class', 'book-item_title');
9898
bookAuthor.setAttribute('class', 'book-item_author');
9999
bookLanguage.setAttribute('class', 'book-item_language');
100-
bookTitle.innerHTML = myBooks[el].title;
101-
bookAuthor.innerHTML = `by ${myBooks[el].author}`;
102-
bookLanguage.innerHTML = `in ${myBooks[el].language}`;
100+
bookTitle.innerText = myBooks[el].title;
101+
bookAuthor.innerText = `by ${myBooks[el].author}`;
102+
bookLanguage.innerText = `in ${myBooks[el].language}`;
103103
book.appendChild(bookTitle);
104104
book.appendChild(bookAuthor);
105105
book.appendChild(bookLanguage);
@@ -116,6 +116,7 @@
116116
const arrayImageUrl = Object.values(bookImages);
117117
console.log(arrayImageUrl);
118118
console.log(arrayImageIds);
119+
119120
function addBookImage() {
120121
for (let i = 0; i < getNrBooks; i++) {
121122
if (getBooks[i].id === arrayImageIds[i]) {

0 commit comments

Comments
 (0)