Skip to content
Advertisement

different vars and ifs for different videos not working

I’m trying to create the function that if a key is pressed, a video appears. I have managed to do it for one key and it works fine:

JavaScript

But i want to do this for several different keys, (26 to be exact). I figured that what I needed to do was copy the same code and place it below, like this:

JavaScript

But i get the error: “‘video’ is already defined. Do not redeclare”. I get that what’s wrong is the composition of the code but I spent a while looking for the right way to do it and couldn’t find it. Any help would be really appreciated!

Advertisement

Answer

A variable can only have one value. When you reassign video, the old value is lost. When the event listeners fire, they’ll use the last value that was assigned, so both keys will play the same video.

You can use different variables, or better would be to use an object that maps keys to videos.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement