-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.js
109 lines (91 loc) · 4.26 KB
/
index.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
async function sprintChallenge5() { // Note the async keyword so you can use `await` inside sprintChallenge5
// 👇 WORK ONLY BELOW THIS LINE 👇
// 👇 WORK ONLY BELOW THIS LINE 👇
// 👇 WORK ONLY BELOW THIS LINE 👇
// 👇 ==================== TASK 1 START ==================== 👇
// 🧠 Use Axios to GET learners and mentors.
// ❗ Use the variables `mentors` and `learners` to store the data.
// ❗ Use the await keyword when using axios.
let mentors = [] // fix this
let learners = [] // fix this
// 👆 ==================== TASK 1 END ====================== 👆
// 👇 ==================== TASK 2 START ==================== 👇
// 🧠 Combine learners and mentors.
// ❗ At this point the learner objects only have the mentors' IDs.
// ❗ Fix the `learners` array so that each learner ends up with this exact structure:
// {
// id: 6,
// fullName: "Bob Johnson",
// email: "bob.johnson@example.com",
// mentors: [
// "Bill Gates",
// "Grace Hopper"
// ]`
// }
// 👆 ==================== TASK 2 END ====================== 👆
const cardsContainer = document.querySelector('.cards')
const info = document.querySelector('.info')
info.textContent = 'No learner is selected'
// 👇 ==================== TASK 3 START ==================== 👇
for (let learner of learners) { // looping over each learner object
// 🧠 Flesh out the elements that describe each learner
// ❗ Give the elements below their (initial) classes, textContent and proper nesting.
// ❗ Do not change the variable names, as the code that follows depends on those names.
// ❗ Also, loop over the mentors inside the learner object, creating an <li> element for each mentor.
// ❗ Fill each <li> with a mentor name, and append it to the <ul> mentorList.
// ❗ Inspect the mock site closely to understand what the initial texts and classes look like!
const card = document.createElement('div')
const heading = document.createElement('h3')
const email = document.createElement('div')
const mentorsHeading = document.createElement('h4')
const mentorsList = document.createElement('ul')
// 👆 ==================== TASK 3 END ====================== 👆
// 👆 WORK ONLY ABOVE THIS LINE 👆
// 👆 WORK ONLY ABOVE THIS LINE 👆
// 👆 WORK ONLY ABOVE THIS LINE 👆
card.appendChild(mentorsList)
card.dataset.fullName = learner.fullName
cardsContainer.appendChild(card)
card.addEventListener('click', evt => {
const mentorsHeading = card.querySelector('h4')
// critical booleans
const didClickTheMentors = evt.target === mentorsHeading
const isCardSelected = card.classList.contains('selected')
// do a reset of all learner names, selected statuses, info message
document.querySelectorAll('.card').forEach(crd => {
crd.classList.remove('selected')
crd.querySelector('h3').textContent = crd.dataset.fullName
})
info.textContent = 'No learner is selected'
// conditional logic
if (!didClickTheMentors) {
// easy case, no mentor involvement
if (!isCardSelected) {
// selecting the card:
card.classList.add('selected')
heading.textContent += `, ID ${learner.id}`
info.textContent = `The selected learner is ${learner.fullName}`
}
} else {
// clicked on mentors, we toggle and select no matter what
card.classList.add('selected')
if (mentorsHeading.classList.contains('open')) {
mentorsHeading.classList.replace('open', 'closed')
} else {
mentorsHeading.classList.replace('closed', 'open')
}
if (!isCardSelected) {
// if card was not selected adjust texts
heading.textContent += `, ID ${learner.id}`
info.textContent = `The selected learner is ${learner.fullName}`
}
}
})
}
const footer = document.querySelector('footer')
const currentYear = new Date().getFullYear()
footer.textContent = `© BLOOM INSTITUTE OF TECHNOLOGY ${currentYear}`
}
// ❗ DO NOT CHANGE THIS CODE. WORK ONLY INSIDE TASKS 1, 2, 3
if (typeof module !== 'undefined' && module.exports) module.exports = { sprintChallenge5 }
else sprintChallenge5()