Hello everyone I’m doing this music player and this is the song loader but the problem is that when I try to assing the value to song constant with lookSongbyId
function it returns me an error idk why
JavaScript
x
28
28
1
let queue = [
2
{
3
id: 1,
4
name: 'Crush',
5
artist: 'Glades',
6
}
7
]
8
9
const loadSong = (id) =>{
10
11
12
function lookSongbyId(id)
13
{
14
queue.forEach(currentSong => {
15
if(currentSong.id == id )
16
{
17
return currentSong
18
}
19
})
20
}
21
22
const song = lookSongbyId(id)
23
24
25
console.log(`la canción ${song.name} ha sido cargada`)
26
}
27
loadSong(1)
28
song
constant is undefined, and I dont know why aghhh
If u could help me with this code I’ve been so thankful with u :DDD
Advertisement
Answer
Assuming the functionlookSongbyId
is just misspelled (you must write function lookSongbyId
), the forEach
function cannot be used to return a value, as indirectly said here.
Use a for ... of
or .find()
to retrieve the element