Day 5 - DOM Events and Clicking Buttons

I have learnt a lot today and it feels satisfying to consolidate past concepts and begin to create things using JS. Here are some of the things I have achieved:



Day 6 - New knowledge (Event handler functions) - Old knowledge (Arrays, functions, random numbers)

planet earth silly

Planet Earth



Day 7 - Code tidying and Multiplying

JS code
Above is the code for the changing rainbow color image I did above yesterday. It worked but I knew the code could be more concise and tidier...
JS code
  • I used the .length property on the array instead of the number 7
  • I reduced the separate functions into on changeText function
  • I made sure that whilst the randomNumber variable is declared globally, it has to be generated within the changeText function so that each time you click, a new number is generated
  • I put the snips of code and this list in a table. I haven't learnt how to use tables properly yet so it was a quick trial and error hack (I get the idea though, seems pretty straight forward)

Multiplier

Below is me playing around with the event handlers and onclick function again. I tried to find a way of using an input box and then using the input value to save to a variable. That would have enabled me to create a scenario for creating a times table quiz question and writing a function to evaluate it and give feedback on the correct or incorrect answer. I tried simply using an input value in the table data with the idea of then updating it using innerHTML but I couldn't make it work and it felt like I was barking up the wrong tree. A few google searches later and I ran out of time but I think it's one of those to park and return to with a bit more knowledge #friday.

1st Number

x

2nd Number

=

Enter Your Answer

Correct Answer

#Weekend

I couldn't resist working it out. Turns out I was barking up the - totally correct tree - All I was doing wrong was including let userInput = document.getElementById('user-input').value; as a global variable instead of retrieving the user-input element and setting it to userInput variable after the button was clicked. That is why I kept getting zero returning all the time. It was bugging me because this is such a key piece of functionality. As a beginner wanting to apply and demonstrate some skills, being able to use an input from a user in a text box and have fun with those behind the scenes in JS is what I have been wanting to do. Monday I will start a new page and work on a proper times tables quiz with as many bells and whistle as I can muster!