Selecting Random Value From An Array Using JavaScript

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:

[code lang=”javascript”]
function randomValueFromArray(array){
var random = Math.floor(Math.random()*array.length);
return array[random];

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 πŸ™‚

Learning JavaScript Using Console

Daily post #7 – I missed writing a post yesterday so I’m going to write two posts today. This one is about using your Google Chrome console to learn JavaScript. Let’s dive into this directly!

First open your Google Chrome console by going to menu bar > view > developer > JavaScript console. Or you can use CMD+OPT+J (because I like keyboard shortcuts).

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:

[code lang=”javascript”]
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:

[code lang=”javascript”]
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! πŸ™‚

Hiding Stuff Quickly Using CSS

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! πŸ™‚

Changing Text Using Button With JavaScript

Daily post #5 – Hey there, today I’ll be writing about changing some text on the web page when a button is pressed. All of this done using JavaScript of course!

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 πŸ™‚

Printing File Names In Folder Using Python

Daily post #4 – Today I’m going to share how you can use Python code to fetch file names inside a specified directory.

[code lang=”python”]
import os

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

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! πŸ™‚