-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
118 lines (104 loc) · 4.29 KB
/
script.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function cleanCourseData(data) {
if (!data || data.length === 0) {
throw new Error("No course data provided.");
}
let idSet = new Set();
let nameSet = new Set();
return data.map(course => {
const cleanedLessons = course.university_lessons.map(lesson => {
const filteredTopics = lesson.university_topics.filter(topic => {
if (!idSet.has(topic.id) && !nameSet.has(topic.name)) {
idSet.add(topic.id);
nameSet.add(topic.name);
return true;
}
return false;
});
filteredTopics.sort((a, b) => a.name.localeCompare(b.name));
return {
...lesson,
university_topics: filteredTopics
};
}).filter(lesson => lesson.university_topics.length > 0);
return {
...course,
university_lessons: cleanedLessons
};
});
}
function renderContent(filteredData) {
let contentHTML = "";
if (!filteredData || filteredData.length === 0) {
contentHTML = `
<div class="card bg-base-200 shadow-xl">
<div class="card-body">
No courses or lessons found.
</div>
</div>`
} else {
$.each(filteredData, function (index, course) {
$.each(course.university_lessons, function (index, category) {
let accordions = "";
$.each(category.university_topics, function (i, topic) {
if (topic.practice_problems.length !== 0) {
let links = topic.practice_problems.map(function (problem) {
return `<a class="btn m-1 btn-outline btn-sm" target="_blank" href="https://citu.codechum.com/student/study-area/${problem.id}">${problem.name}</a>`;
}).join("");
accordions += `
<div class="collapse collapse-arrow rounded-box bg-base-100">
<input type="checkbox" id="acc${topic.id}">
<div class="collapse-title text-xl font-medium">${topic.name} <span class="badge badge-ghost">${topic.practice_problems.length} Items</span></div>
<div class="collapse-content">
<ul class="list-disc ml-5">
${links}
</ul>
</div>
</div>`;
}
});
if (accordions) {
contentHTML += `
<div class="card bg-base-200 shadow-xl">
<div class="card-body">
<div class="divider text-3xl">
${category.name}
</div>
${accordions}
</div>
</div>`;
}
});
});
}
$('#content').html(contentHTML);
}
function filterCourseData(data, query, exactMatch = false) {
if (!data || data.length === 0) {
return [];
}
const filteredData = data.map(course => {
const filteredLessons = course.university_lessons.map(lesson => {
const filteredTopics = lesson.university_topics.map(topic => {
const filteredProblems = topic.practice_problems.filter(problem =>
exactMatch ? problem.name === query : problem.name.toLowerCase().includes(query)
);
return {
...topic,
practice_problems: filteredProblems
};
}).filter(topic => topic.practice_problems.length > 0);
return {
...lesson,
university_topics: filteredTopics
};
}).filter(lesson => lesson.university_topics.length > 0);
return {
...course,
university_lessons: filteredLessons
};
}).filter(course => course.university_lessons.length > 0);
if (filteredData.length === 0) {
console.warn("No matching data found for the given query.");
}
return filteredData;
}