Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (78 loc) · 2.34 KB
/
Copy pathapp.js
File metadata and controls
98 lines (78 loc) · 2.34 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
// 1. Import the complete book array from your data file
import catalog from './catalogData.js';
// 2. Group Books by Decade Function
function groupByDecade(catalog) {
const grouped = {};
for (let i = 0; i < catalog.length; i++) {
const book = catalog[i];
if (book.year === "Unknown") {
if (!grouped["Unknown"]) {
grouped["Unknown"] = [];
}
grouped["Unknown"].push(book);
continue;
}
const decade = Math.floor(book.year / 10) * 10;
const decadeKey = `${decade}s`;
if (!grouped[decadeKey]) {
grouped[decadeKey] = [];
}
grouped[decadeKey].push(book);
}
return grouped;
}
// 3. Render Library Card Component Function
function renderEntry(entry) {
const title = entry.title || "Unknown";
const author = entry.author || "Unknown";
const year = entry.year || "Unknown";
const location = entry.location || "Unknown";
return `${"-".repeat(25)}
Title: ${title}
Author: ${author}
Year: ${year}
Location: ${location}
${"-".repeat(25)}`;
}
// 4. Data Validation Gatekeeper Function
function validateEntry(entry) {
let isValid = true;
if (!entry.title || entry.title === "Unknown") {
isValid = false;
}
if (!entry.author || entry.author === "Unknown") {
isValid = false;
}
if (!entry.year || entry.year === "Unknown") {
isValid = false;
}
if (!entry.location || entry.location === "Unknown") {
isValid = false;
}
return isValid;
}
// 5. CSV Data Exporter Function
function exportToCSV(catalog) {
const header = "Title,Author,Year,Location";
const rows = [];
for (let i = 0; i < catalog.length; i++) {
const entry = catalog[i];
rows.push(`"${entry.title}","${entry.author}",${entry.year},"${entry.location}"`);
}
let csv = header;
for (let i = 0; i < rows.length; i++) {
csv = csv + "\n" + rows[i];
}
return csv;
}
// ==========================================
// EXECUTION & TESTING
// ==========================================
console.log("--- 1. TESTING VALIDATION ON FIRST ENTRY ---");
console.log(`Is the first book valid? ${validateEntry(catalog[0])}`);
console.log("\n--- 2. PRINTING FIRST BOOK INDEX CARD ---");
console.log(renderEntry(catalog[0]));
console.log("\n--- 3. BOOKS GROUPED BY DECADE ---");
console.log(groupByDecade(catalog));
console.log("\n--- 4. FINAL CSV EXPORT STRING ---");
console.log(exportToCSV(catalog));