Learn JavaScript concepts by building a random quote generator

Learning JavaScript is fun. But What's the point if you are not building stuff with it. So in this article, we will learn javascript basic concepts and create a random quote generator of our own.

I assume you have a basic knowledge of HTML and CSS as the main objective of this article is to learn javascript. Else you can check these tutorials on HTML and CSS. I will try to explain everything in detail and by the time you finish this lesson, you will have an understanding of concepts like strings, arrays, functions, event listeners, template literals in JavaScript. So let's begin.

Getting started

Lets first create three different files in our folder as

  • index.html for creating the UI and displaying the content,
  • style.css to make it attractive by adding styles.
  • script.js will contain the actual code which makes our app work.

1. creating the content for the app

The first step is to create a UI. Here is how our index.html file will look like

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <link rel="shortcut icon" href="icons/favicon.png">
    <title>Random quote</title>
</head>
<body>
    <div class="content">
        <div class="card">
          <h2>Random quote</h2>
          <p>wanna see a random quote ?</p>
          <!--button to display random quote-->
          <button onclick="displayQuote()" class="btn">Generate<button>

        </div>
        <div id="quote">
         <!--Quote will be displayed here.-->
        </div>
    </div>
</body>
<script src="script.js"></script>
</html>

Screenshot (238).png

And this is how it looks like in the browser. Wait a minute, It doesn't look nice right.

2. Style the content

Let's make it appealing by adding our style.css file

body{
     background-color: indigo; 
     margin:auto;
    }

h2{
   background-color: #1c1c1c; 
   color:#ffffff;
   padding:10px;
   font-family: arial;
   text-align: center;
  }

.btn{
     height:50px; 
     width:150px;
     font-size:18px;
     color:#ffffff;
     background-color: dodgerblue;
     border:none;
     padding:5px;
     border-radius:5px;
     }

.card{
      padding:15px 30px 15px 30px;
      margin:40px auto;
      max-width:500px;
      max-height:300px;
      text-align: center;
      background-color: #ffffff;
      border-radius:5px;
     } 

p{
  font-family:'Segoe UI';
  font-weight:400;
  font-size:20px;
 }

img{
    height:25px;
    width:25px;
   }

And boom ! this looks great now.

Screenshot (240).png

Now let's think about how we are going to make it work. We need to figure out a way to display random quotes after clicking a button. And we will go just with the simplest approach in a moment.

Forget coding. let's just think about what we have to do. okay. First, we will need quotes. We will have to store them somewhere and do something such that we can display a random quote after clicking a button. That's it.

3. let's learn some javaScript

Strings

A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types which we are going to use to create quotes.

In JavaScript, there are three ways to write strings. They can be written inside single quotes (' '), double quotes (" "), or backticks ( ). Strings with backticks(``) are called template literals which we will learn later.

'This is a string'

So all our quotes are basically strings like this.

'Stay hungry, stay foolish !'

But there are multiple such strings and we need something to store them. Here comes the concept of Arrays. But what's an array?

Arrays

An array in JavaScript is a type of global object that is used to store data. Arrays consist of an ordered collection or list containing zero or more data types, and use numbered indices starting from 0 to access specific items.

<!--Creating an array-->
let quotes =[];

Arrays can contain any data type, including numbers, strings, and objects. We will be storing our strings in an array like this.

let quotes=[
'The secret to life is to love who you are.',
'Look for opportunities in every change in your life.',
'Persist while others are quitting.',
'and so on.'
];

Array.length

The length property of an object which is an instance of type Array sets or returns the number of elements in that array.

quotes.length;
<!--Output-->
4

Accessing Items in an Array

An item in a JavaScript array is accessed by referring to the index number of the item in square brackets.

quotes[2]; // Output: Persist while others are quitting.

But here we will not access like this. We want a random index to display a random quote. Then how do we do that? We will use the math object.

Using Math object

Math is a built-in object that has properties and methods for mathematical constants and functions.

Screenshot (242).png

If you type math in console, you will see there are so many built-in methods for Math object. We will be using a few of them.

Math.random()

The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1. See how it returns every time we call it in the console.

Screenshot (243).png

But this is not what we want. We want a whole number and not a floating type. For this, the Math object provides another method called Math.floor()

Math.floor()

Math.floor() returns a number representing the largest integer less than or equal to the specified number. So if we pass the math.random() as a parameter to math.floor() we will get a whole number. But Math.floor() is always rounding down to the nearest decimal, therefore, every decimal between 0 and 1 will always revert back to 0.

Screenshot (245).png

In order to get a random number between 0 to the length of our array, we will multiply the math.random() by quotes.length. Finally the random index we want is

let index=Math.floor(Math.random()*quotes.length); // returns a random number that will be stored in our index variable

quotes[index]; // Output: return quote at the `index`

index will always generate a valid array index number for our quotes array. So we figured out how to retrieve a random quote from the quotes array. But we don't want to display just a string. We are going to do it nicely using template literals.

Template Literals

The Template Literal, introduced in ES6, is a new way to create a string. With it comes new features that allow us more control over dynamic strings in our programs.

Template literals are string literals allowing embedded expressions. It can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}).

`string text ${expression} string text`;

HTML Templates

With the ability to have multi-line strings and use Template Expressions to add content into our string, this makes it really nice to use for HTML templates in our code. So we will create a HTML template for our quote and store it in a variable. We used the placeholder to pass our quotes in the template.

let quote=`<div class="card">
<img src="icons/favicon.png">
<p>${quotes[index]}</p> <!--random quote string-->
<img src="icons/favicon.png">
</div>
`;

I added images to make it more appealing.

Query Selectors

To access an element in dom(Document Object Model), we will use the querySelector() method. The Document method querySelector() returns the first element within the document that matches the specified selector. The syntax is

element = document.querySelector(selectors);

In our HTML file, we have a div element with id="quote". We will access it in the same way as this. And then we will add the HTML template we created earlier to this div element.

Element.innerHTML

Each HTML element has an innerHTML property that defines both the HTML code and the text that occurs between that element's opening and closing tag. By changing an element's innerHTML after some user interaction, we can make much more interactive pages.

let div=document.querySelector('#quote');
div.innerHTML=quote;

The code we wrote till now will work only once. And we want to execute this every time we want to display a quote. We can do this using functions.

Functions

A function is a block of code that performs an action or returns a value. Functions are custom code defined by programmers that are reusable, and can, therefore, make your programs more modular and efficient.

Defining a Function

Functions are defined, or declared, with the function keyword. Below is the syntax for a function in JavaScript.

function nameOfFunction() {
    // Code to be executed
}

We will create the displayQuote function add all this code in the function.

function displayQuote(){
let index=Math.floor(Math.random()*quotes.length);

//display the quote of that index
let div=document.querySelector('#quote');
let quote=`<div class="card">
<img src="icons/favicon.png">
<p>${quotes[index]}</p>
<img src="icons/favicon.png">
</div>
`;
div.innerHTML=quote;
}

We are almost done. Just need to invoke this displayQuote function whenever we click the generate button.

Events in javaScript

Events are actions that take place in the browser that can be initiated by either the user or the browser itself.

In our case, It's when the user clicks a button.

Inline event handler

An event handler is a JavaScript function that runs when an event fires.

Directly on the button, we will add an attribute called onclick. The attribute value will be a function we created called displayQuote().

<button onclick="displayQuote()" class="btn">Generate</button>

So displayQuote() function will be called whenever user clicks the generate button and a random quote will be displayed.

Screenshot (248).png

And we are done. congratulations. We built a random quote generator while learning some JavaScript concepts.

Conclusion

So the quote generator is ready. Also if you wish, you can add some more features of your own like tweeting the quote or something like that.

You can find the complete repository of the code on Github.
Also, you can see it live here. I hope you had fun while doing this and learned a few things along the way. Well for me this process of building things helps me a lot while learning.

You can connect with me on Twitter, Github or Linkedin.

⚡ Happy learning !

No Comments Yet