-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (44 loc) · 1.19 KB
/
app.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
const songsList = document.getElementById('songsList');
const searchBar = document.getElementById('searchBar');
let songs = [];
console.log(searchBar);
// After an user type a character in the searchbox
searchBar.addEventListener('keyup', (e) => {
const searchString = e.target.value.toLowerCase();
const filteredSongs = songs.filter( song => {
console.log(song.title);
console.log(song.artist);
return (
song.title.toLowerCase().includes(searchString) ||
song.artist.toLowerCase().includes(searchString) ||
song.album.toLowerCase().includes(searchString)
);
});
displaysongs(filteredSongs);
// console.log(filteredsongs);
});
const loadsongs = async () => {
try {
const res = await fetch('./public/songs.json');
songs = await res.json();
displaysongs(songs);
console.log(songs);
} catch (err) {
console.error(err);
}
};
const displaysongs = (songs) => {
const htmlString = songs
.map((song) => {
return `
<li class="song">
<h2>${song.title}</h2>
<p>${song.artist}</p>
<img src="${song.album_image}"></img>
</li>
`;
})
.join('');
songsList.innerHTML = htmlString;
};
loadsongs();