Some important concept of JavaScript

Some important concept of JavaScript

ยท

5 min read

Using concept like

๐Ÿ‘‰ For loops

๐Ÿ‘‰ Dom Manipulation

๐Ÿ‘‰ Variables

๐Ÿ‘‰ Conditionals (if else if)

๐Ÿ‘‰ Template Literals

๐Ÿ‘‰ Event Listeners

๐Ÿ‘‰ Higher order Function (Math.random())

using these concepts and create a random quote and displaying it on the web page

<!DOCTYPE html>
<html>
<head>
  <title>Random Quote Generator</title>
</head>
<body>
  <h1>Random Quote Generator</h1>
  <p id="quoteDisplay"></p>
  <button id="quoteButton">Generate Quote</button>

  <script>
    // Array of quotes
    const quotes = [
      "The only way to do great work is to love what you do. - Steve Jobs",
      "Innovation distinguishes between a leader and a follower. - Steve Jobs",
      "Your time is limited, don't waste it living someone else's life. - Steve Jobs",
      "The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela",
      "The best way to predict the future is to create it. - Peter Drucker",
      "The secret of getting ahead is getting started. - Mark Twain",
      "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
      "The best revenge is massive success. - Frank Sinatra"
    ];

    // Get DOM elements
    const quoteDisplay = document.getElementById("quoteDisplay");
    const quoteButton = document.getElementById("quoteButton");

    // Event listener for button click
    quoteButton.addEventListener("click", generateQuote);

    // Function to generate random quote
    function generateQuote() {
      // Get a random index from the quotes array
      const randomIndex = Math.floor(Math.random() * quotes.length);

      // Display the random quote
      quoteDisplay.textContent = quotes[randomIndex];
    }
  </script>
</body>
</html>

In this example, we use a for loop to iterate through the array of quotes, and a conditional statement (if else if) to check if a quote has been generated. We also use variables to store DOM elements and the randomly generated index. Template literals are used to display the generated quote in the HTML. We add an event listener to the button for a click event, which triggers the generateQuote function when the button is clicked. Finally, we use the Math.random() function as a higher-order function to generate a random index from the quotes array.

concept used in this code:

  1. For loops: In the example, we do not use a for loop explicitly. However, we use the for loop concept implicitly when we generate a random index to access a quote from the array of quotes. The line const randomIndex = Math.floor(Math.random() * quotes.length); uses Math.random() to generate a random decimal number between 0 and 1, and then multiplies it by the length of the quotes array. The Math.floor() function is used to round down the decimal number to the nearest integer, which gives us a random index within the range of the array length. This is an example of using the concept of a loop to generate a random index for accessing array elements.

  2. Dom Manipulation: DOM (Document Object Model) manipulation refers to the process of modifying the content or structure of an HTML document using JavaScript. In the example, we use DOM manipulation to display the randomly generated quote on the webpage. We select the DOM element with the id "quoteDisplay" using document.getElementById("quoteDisplay"), and then set its textContent property to the randomly selected quote, which updates the content of the element with the generated quote.

  3. Variables: Variables are used to store and manipulate data in JavaScript. In the example, we use variables to store the DOM elements, the array of quotes, and the randomly generated index. For example, we use const quoteDisplay = document.getElementById("quoteDisplay"); to store the DOM element with the id "quoteDisplay" in a variable called quoteDisplay, which allows us to easily access and modify the content of that element later in the code.

  4. Conditionals (if else if): Conditional statements are used to execute different code blocks based on a condition. In the example, we use a conditional statement to check if a quote has been generated or not. If a quote has been generated, we update the textContent of the DOM element with the generated quote. If a quote has not been generated, the textContent will be empty, and the user will see an empty space until they click the "Generate Quote" button.

  5. Template Literals: Template literals are a way to embed expressions inside string literals in JavaScript. In the example, we use template literals to display the randomly generated quote in the HTML by setting the textContent of the DOM element with the id "quoteDisplay" to the generated quote. The template literal syntax ${} allows us to insert the value of the quotes[randomIndex] expression directly into the string, making it easier to concatenate values and create dynamic strings.

  6. Event Listeners: Event listeners are used to listen for specific events (such as a button click or a key press) and trigger a function when the event occurs. In the example, we use an event listener to listen for a click event on the "Generate Quote" button. When the button is clicked, the event listener triggers the generateQuote function, which generates a random quote and updates the DOM accordingly.

  7. Higher Order Function (Math.random()): Higher-order functions are functions that take one or more functions as arguments or return a function as a result. In the example, Math.random() is a higher-order function that returns a random decimal number between 0 and 1. We use it as a higher-order function to generate a random index for accessing the quotes array by multiplying the random decimal number with the length of the array and rounding it down using Math.floor(). This allows us to generate a random index within the range of the array length, which is then used to retrieve a randomly selected quote from the array.

building rock paper scissors through this concept:

you can try to build a working element of rock paper scissors game through this concept

credits

chatgpt for comments on the code and clever programming js tutorials

ย