forked from truecodersio/JavaScript_Objects_Arrays
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (63 loc) · 1.7 KB
/
app.js
File metadata and controls
98 lines (63 loc) · 1.7 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
// Exercise 1 Section
const num = [2, 22, 12, 17, 18, 39, 129];
function arraySum(arr) {
let sum = 0;
arr.forEach((number) => {
sum += number;
});
return sum;
}
console.log(arraySum(num));
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 1);
console.log(sum); // Output: 16
// Exercise 2 Section
let favBook = {
};
favBook.title = "book title";
favBook.author = " fav author";
favBook.pageCount = 300;
favBook.readCount = 1;
// console.log(favBook);
// favBook.info = function () {
// return `${this.title} by ${this.author} is ${this.pageCount}`
// }
function info() {
return `${this.title} by ${this.author} is ${this.pageCount} pages`;
}
favBook.info = info;
let favBook2 = {
title: "2nd fav book",
author: "2nd author name",
pageCount: 300,
readCount: 1,
info,
};
console.log(favBook.info());
console.log(favBook2.info());
// Exercise 3 Section
let sentence = "The quick brown fox jumps over the lazy dog";
let words = sentence.split(" ");
for (let i = 0; i < words.length; i++) {
let characters = words[i].split("");
let reversedCharacters = characters.reverse();
words[i] = reversedCharacters.join("");
}
let reversedSentence = words.join(" ");
console.log(reversedSentence);
// Exercise 4 Section
let csvData = "name,age\nFrodo,50\nSam,38\nMerry,36\nPippin,26";
const rows = csvData.split('\n');
const headers = rows[0].split(',');
const result = [];
for (let i = 1; i < rows.length; i++) {
const rowData = rows[i].split(',');
const obj = {};
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = rowData[j];
}
result.push(obj);
}
console.log(result);