Tell us what’s happening: how to assign result of find method.
Your code so far
const playSong = (id) => {
const song = find(song)
};
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Learn Basic String and Array Methods by Building a Music Player - Step 30
If you use this example, from the step, as guidance (replacing what is necessary with the information provided in the instructions, your code should pass.
const foundNumber = numbers.find((number) => number > 25);
To assign the result of the find method to a variable, you can simply do it like this:
const playSong = (id) => {
const song = array.find(item => item.id === id);
// Use the 'song' variable here
};
In the above code:
array is the array you want to search in.
find is a method that takes a callback function. This function is applied to each element in the array until one is found where the callback returns true. It returns the first matching element, or undefined if no match is found.
Inside the callback function, item represents each element of the array as the find method iterates over it.
item.id === id is the condition we’re checking. It compares the id property of each element with the id passed to the playSong function.