Learn Basic String and Array Methods by Building a Music Player - Step 28

Lesson does not explain the addEventListener method.

I was having issues with this step since it says to hook the playButton element, so I assumed to use an onclick like the previous project. It then said to use addEventListener() which has not been brought up previously. I tried to use it, but it does not specify that “click” need to be a string, and that it requires () as another parameter. If more guidance could be added to this step, and explanation on how to use addEventListener(), that would be great.

Spoiler code for step answer:

playButton.addEventListener('click', () => {});

Learn Basic String and Array Methods by Building a Music Player - Step 28

2 Likes

HI @Innocent-Traitor !

Welcome to the forum!

The addEventListener method was introduced in the previous project here

it is best to do these projects in the order listed so you don’t miss out on new concepts introduced.

Hope that helps

1 Like

Hi,

I see the spoiler code for this step is
playButton.addEventListener(‘click’, () => {});
but I don’t understand why empty parenthesis - () in the addeventListener instead of playsong(), please clarify

here is my code: playButton.addEventListener(“click”, playSong() => {});

I also agree that this step is not explained very well. The playSong reference is such a boondoggle. If it isn’t intended for us to use it in this step, then I would expect something like “But first let’s create …” I was Google searching and trying to find something in the MDN or W3 websites.

1 Like

Agree +1. I’m doing the projects in order and still don’t understand why empty parentheses are used here. My assumption is at some point arrow syntax is used to replace a lot of things we learned previously and it’s causing some confusion. Agree with the above the instructions could be clearer, i.e. “don’t add the playSong function in this step”

I have gone ahead and created an issue for this to help clarify what is expected

So the parenthesis here are part are where you would place the parameter if you had any

() => {}

in this case, this callback function does not have any parameters. But if we tried to removed the parenthesis here, then I would get syntax errors

someElement.addEventListener('click', => {})

The only time I could remove the parenthesis for the callback arrow function is if I had one parameter like this

someElement.addEventListener('click', param => {})

If your callback function had multiple parameters, then you would need to add in the parenthesis like this

someElement.addEventListener('click', (param1, param2) => {})

I understand the confusion in the new way of writing functions. It took a while for me to get adjusted when I first used them too.

The first few projects focused on traditional function declarations

function myFunction() {
// some code here
}

but the reason for introducing arrow functions in this project, is because they are used a lot in javascript code out in the wild. So it is important to learn about them and be comfortable with the syntax.

There are situations where you might prefer one over the other.
I think this article gives a good intro into those differences

hope that helps

3 Likes

Hi @danielbailey0629 and @sphc648brr !

Welcome to the forum

I have created an issue mentioned in earlier comments that will clarify the instructions better so learners will better understand what is expected.

1 Like

Thank you so much for following up and explaining the arrow functions! I just completed the project and after practicing more in the remaining steps, I feel like I have a better grasp on writing functions in this new way.

1 Like

The part that is confusing me is that I was under the impression that arrow functions had to be the value assignment of a variable. Is the addEventListener() declaring a new variable or am I just mistaken in my understanding?

Sometimes, but not always.

Here is an example of a named arrow function

const myNamedFunction = () => {
  // some code here
};

but sometimes you can use arrow functions as a callback like this

const exampleElement = document.getElementById('demo')
exampleElement.addEventListener('click', () => {
  // random code goes here
});

No, the event listener is listening for a particular event.
An example of an event would be a click event. (Ex. when a user clicks on a button)

When a click event is detected, then something should happen.
In this case, you can have a callback function which will execute some code when a click event is detected.

This would be an example of an anonymous arrow function which serves as the callback to this event.

btn.addEventListener("click", () => alert("You just clicked this button"));

If you play around with this codepen example, you will see that each time the button is clicked it will show an alert message.

hope that helps

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.