This content originally appeared on DEV Community and was authored by RedDev
This past Tuesday I had an abnormal amount of downtime at my current job. I decided to use that time to play with JavaScript on my own. I've seen other background color changing projects and figured I'd take a swing.
I'm in the middle of a website project for a co-worker. My friend and code mentor helped me with a mobile menu using JS for the project. Using that function as a starting point, I tried changing the background color with an on-click event. With no success, I turned to Google and found a simple way to do what I wanted using document.body.style.background
.
Then it was time to tackle the text color change on the h1 element. After some more Google, I found document.getElementById().style.color
to work nicely.
Everything was functional and I excitedly sent my friend the CodePen link, which at the time was the same code as this post's cover image- five nearly identical functions and all. It was gratifying to hear that it looked good, but he then said I could do the same thing with only one function. After all that work!?
One Zoom call and some explaining later, it made sense! Arguments, "Ah-ha!" I remembered using them in FreeCodeCamp exercises, but it's much different when you're actually building something. Now I have a better idea of how arguments and JavaScript in general works. It was frustrating but the payoff was totally worth the aggravation.
I don't know if you need to hear this, but do not give up. Just keep trying. This small project has allowed me to connect dots and put some JavaScript things into perspective. I'm excited for the future and eager to keep going.
Here is the single function background color changer. I hope you have as much fun as I do clicking those buttons. :)
This content originally appeared on DEV Community and was authored by RedDev
RedDev | Sciencx (2021-05-14T00:53:18+00:00) Writing functional JS as a newbie. Retrieved from https://www.scien.cx/2021/05/14/writing-functional-js-as-a-newbie/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.