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! 🙂
Daily post #6 – I’m late to write my daily post so I’m going to write a short tip (but a useful one) for this one. This is about how to hide something quickly on your site using CSS.
There’s a CSS property called display whose value can be set to none for any HTML element that you wish to hide. Here’s how:
Visit your site in Chrome, right click on the point which you wish to hide and select inspect element. Find the class or id and then make use of the code given above.
And it’ll magically hide the element from the page! 🙂
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 🙂
Daily post #4 – Today I’m going to share how you can use Python code to fetch file names inside a specified directory.
indir = ‘/users/omkarbhagat/desktop/batman/’
for root, dirs, filenames in os.walk(indir):
for x in filenames:
x = "/users/omkarbhagat/desktop/batman/"+x
Note that the python code above only works with correct indentation so if something breaks, make sure to have correct indentation. Also make sure to have all spaces or all tabs in the code, don’t mix them up!
I had the following setup – A folder called batman on my desktop with couple of txt files inside it. Running the above code returns the following result –
Omkars-MacBook-Pro-3:desktop omkarbhagat$ python test.py
Once I have a file name or directory in a variable, I can do stuff with it. For example, I can change all file names, or edit the content of all these files by a conditional statement or something. I’ll probably share that in a separate post. That’s all for today, thanks for reading! 🙂