Daily post #11 – I just wrote a piece of code to generate a random hex color code. Here’s what it looks like:
- Create a box and a button
- Select those using JS
- Add event listener to the button
- Connect it with updateButton function
- This function will generate hex code and insert it inside the box.
My logic was to generate a random number from 0 to 9.
Daily post #9 – If you right click on this page and click inspect element, you’ll notice a $0 symbol next to the element which is currently selected. Here’s a screenshot for reference –
So what is it and what it really does? I haven’t explored it enough but based on what I learned it helps us as a reference point. To understand what this means, you have to click on the console tab in developer tools (or simply press CMD+OPT+J on Mac).
That’s pretty much what it does! (afaik at this point) 🙂
Daily post #8 – In this post, I’ll be sharing a simple trick to select or print a random value from a given array. Here’s the code for it:
var random = Math.floor(Math.random()*array.length);
My favorite part of this code is the math on the second line – multiplying a random number with the length of the array. This is genius! I’ll explain why –
Math.random function generates a number between 0 and 1 (but never 1). If you multiply such a number with any given number X, the result will never be greater than X. And thus you will never run into an ArrayIndexOutOfBounds error 🙂
Once in Console, you can type in any JS code and it’ll execute right there and give you the result at the same time. For example, type in the following code:
a = 5
b = 6
This will print the values of a and b on screen. You can also write multiline code using shift+enter or just by copy pasting code in there. For example:
for (i=0; i<10; i++)
This will print out numbers from 0 to 9. The best part about this process is you can do this inside your browser while watching a tutorial on YouTube!
That’s all in this post. Going to write another post now! 🙂
And it does this:
Very cool, right? Just a button changing some text using JS! Nothing great but pretty Awesome 🙂
We first made JS understand that btn is the button which we click and txt is the paragraph tag containing some text which should change when the button is clicked.
We then added an EventListener to btn so we know as soon as it gets clicked. And that triggers the code inside the function.
That code checks if txt is Awesome, make it Not Awesome and vice versa! That’s all 🙂