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
69 lines (52 loc) · 1.6 KB
/
app.js
File metadata and controls
69 lines (52 loc) · 1.6 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
console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("EXERCISE 1:\n==========\n");
const number = [2, 22, 12, 17, 18, 39, 129];
function arraySum(arr){
let sum = 0;
arr.forEach((number) => (sum += number));
return sum;
}
console.log(arraySum(number));
// Exercise 2 Section
console.log("EXERCISE 2:\n==========\n");
const favBook = {};
favBook.title = "Pride and Prejudice";
favBook.author = "Jane Austen";
favBook.pageCount = 504;
favBook.readCount = 1;
console.log(favBook);
favBook.info = function () {
return `${this.title} by ${this.author} is ${this.pageCount} page(s) and has been read ${this.readCount} time.`;
};
console.log(favBook.info());
//Exercise 3 Section
console.log("EXERCISE 3:\n==========\n");
let sentence = "The quick brown fox jumps over the lazy dog";
let splitUpWords = sentence.split(" ");
let result = "";
for (let word of splitUpWords){
let myReversedWord = "";
for (let i = word.length - 1; i >= 0; i--){
myReversedWord += word[i];
}
result += (myReversedWord + " ");
}
console.log(result.trim());
//Exercise 4 Section
console.log("EXERCISE 4:\n==========\n");
let csvData = "name,age\nFrodo,50\nSam,38\nMerry,36\nPippin,26";
let dataSplit = csvData.split("\n");
const headers = (dataSplit[0].split(","));
console.log(headers);
const resultObjects = [];
for (i = 1; i < dataSplit.length; i++){
const values = dataSplit[i].split(",");
let obj = {};
for (let j = 0; j< headers.length; j++){
obj[headers[j]] = values[j];
}
console.log(obj);
resultObjects.push(obj);
}
console.log(resultObjects);