Monday, 5 June 2023

5 Must-Have JavaScript Programs for Web Development || Learn JavaScript

 

  5 Must-Have JavaScript Programs for Web Development  

   

Introduction:

JavaScript is a versatile programming language that powers the interactivity and dynamic nature of modern websites. It is widely used by web developers to create engaging user interfaces, perform complex calculations, and manage data on the client-side. In this article, we will explore five essential JavaScript programs that every web developer should have in their toolkit. These programs will enhance your productivity, improve the user experience, and simplify common tasks in web development.


1. Form Validation:

Form validation is crucial for ensuring data integrity and improving user experience. JavaScript provides powerful tools for validating user input before it is submitted to the server. By writing custom JavaScript functions, you can perform real-time validation on form fields, such as checking for required fields, validating email addresses, and enforcing specific formats. This program will help you create forms that prevent erroneous data from being submitted, reducing the chances of user frustration and data corruption.


2. Image Sliders:

Image sliders or carousels are popular components in modern web design. They allow you to showcase multiple images or content in a visually appealing and interactive manner. JavaScript frameworks like jQuery or libraries like Swiper.js provide easy-to-use APIs for creating dynamic image sliders. With JavaScript, you can add features like auto-play, navigation controls, and smooth transitions, enhancing the visual appeal and engagement of your website.


3. Real-time Search:

Implementing a real-time search feature can greatly improve the user experience of websites with large amounts of data. JavaScript's event-driven nature makes it an ideal choice for creating dynamic search functionality. By binding JavaScript events to search input fields, you can trigger live searches as users type, updating the search results in real-time without needing to refresh the page. This program will help you create search interfaces that are fast, responsive, and user-friendly.


4. Drag-and-Drop Interactions:

Drag-and-drop functionality allows users to intuitively interact with web content by dragging elements and dropping them onto specific areas or targets. JavaScript provides native APIs like Drag and Drop API or popular libraries like interact.js, which simplify the implementation of drag-and-drop interactions. This program will enable you to create interactive interfaces where users can reorder lists, upload files by dragging them onto a designated area, or create custom workflows through intuitive drag-and-drop gestures.


5. Client-side Data Storage:

JavaScript also enables you to store and manage data on the client-side without relying solely on server-side databases. With the help of JavaScript APIs like Web Storage (localStorage and sessionStorage) or IndexedDB, you can store user preferences, cached data, or even create offline web applications. This program will help you harness the power of client-side data storage, improving performance, reducing server load, and enabling users to access your website even without an internet connection.


Conclusion:

JavaScript is a powerful programming language that empowers web developers to create rich, interactive, and engaging websites. By incorporating these five essential JavaScript programs into your web development toolkit, you can enhance user experience, improve productivity, and tackle common challenges efficiently. Whether you are validating forms, creating image sliders, implementing real-time search, enabling drag-and-drop interactions, or managing client-side data storage, JavaScript has you covered. So dive into these programs, unleash your creativity, and take your web development skills to new heights.

JavaScript Question Practice For Logic Building || JS QNA

 JavaScript Question Practice For Logic Building 


 Q .  Write a JavaScript program to check whether
         a given integer is within 20 of 100 or 400.
     solution
    function testhundred(x) {
      return ((Math.abs(100 - x) <= 20) ||
       (Math.abs(400 - x) <= 20));
    }
    console.log(testhundred(10));
    console.log(testhundred(90));
     

     Q .  Write a JavaScript program to check two given
     integers whether one is positive and another one is negative.  
    solution
    function check(a,b){
      if((a < 0 && b > 0) || a > 0 && b < 0) {
        return true ;
      }
      else{
        return false;
      }
    }
    console.log(check(2,2))
    console.log(check(2,-2))
    console.log(check(-2,2))
    console.log(check(-2,-2))
     
     Q. Write a JavaScript program to create another string
    by adding "Py" in front of a given string. If the given
    string begins with "Py" return the original string.
     Solution
    function string_check(str1) {
    if (str1 === null || str1 === undefined || str1.substring(0, 2) === 'Py')
      {
        return str1;
      }
      return "Py"+str1;
    }
   
    console.log(string_check("Python"));
    console.log(string_check("thon"));
   
    Q . Write a JavaScript program to remove a character at the
   specified position in a given string and return the modified string
    Solution
    function removestr(){
      let str1 = "Hi getcodr" ;
      let str2 = str1.replace('Hi ','') ;
      return str2 ;
    }
    console.log(removestr()) ;
   
    Q. Write a JavaScript program to create a new string from a
       given string by changing the position of the first and last
     characters. The string length must be broader than or equal to 1.
    Solution
      function first_last(str1){
      if(str1.length <= 1){
        return str1 ;
      }
      newstr = str1.substring(1, str1.length - 1) ;
      return (str1.charAt(str1.length - 1)) + newstr + str1.charAt(0) ;
    }
    console.log(first_last('ram'));

Sunday, 4 June 2023

Coding Wisdom and Humor: 20 Inspirational Quotes for Programmers || Coding Quotes

 


1 . Why did the JavaScript developer go broke? Because  he lost his cache .     


2 . The sooner you start to code, the longer the program will take.    


3. It's not a bug - it's an undocumented feature.    


4 . The only valid measurement of code quality: WTFs/minute.    


5 . Java is to JavaScript what car is to carpet.    


6 . Why do JavaScript developers prefer dark mode?    

Because they like to console.log in the dark.    


7 . Why do JavaScript programmers prefer functional programming?    

Because they don't like classical music.    


8 . I'm not superstitious, but I am a little stitious.    


9 . If you think technology can solve your problems,     

then you don't understand technology.    


10 . There are 10 types of people in the world: those who understand binary,    

and those who don't.    


11 . Knock, knock. Who's there? Java. Java who? JavaScript.    


12 . Why did the programmer quit his job? Because he didn't get arrays.    



13 . Programming is like sex. One mistake and                                                                    you have to support it for the rest of your life.     


14 . Why did the developer go broke? Because     

he couldn't find an open-source business model.    


15 . Why do programmers prefer dark mode? Because     

light attracts bugs.    


16 . Why do JavaScript developers prefer coffee? Because      

they don't like Java.    


17 . I have a joke about an infinite loop, but I'm afraid       

it will never end.    


18 . Why do programmers prefer iOS development? Because    

they don't like using Android.    


19 . Why do JavaScript developers always get lost? Because     

they can't find their way through the callback hell.    


20 . Why did the developer go broke? Because he    

     spent all his cache on JavaScript     frameworks.    



JavaScript Practice: 10 Questions with Solutions || JS Practice

 

         
        

Q.  Write a JavaScript program to find out if
         1st January will be a Sunday between 2014 and 2050.
          solution
           for(year = 2000;year<=2050 ;year++){
         let dat = new Date(year,0,1) ;
          if(dat.getDay() == 0){

         console.log("1st January will be a Sunday" + year) ;
       }
       }

        Q.   Write a JavaScript program where the program takes a
        random integer between 1   and 10, and the user is  then
         prompted to input a guess number. The program displays a
       message "Good Work" if the input matches the guess number
       otherwise "Not matched".
        Solution
          let rand = Math.ceil(Math.random()*5) ;
       let num = prompt("Please enter a number Betwwen 1 to 5") ;
       num = Number(num) ;
       if(num == rand){
         document.write("Good Work You Guess the Right Number");
       }
       else{
         document.write(`Not Matched ,the number was  ${rand} `);
         }
       console.log(rand)
 
 
       Q. Write a JavaScript program to calculate multiplication
        and division of two numbers (input from the user).  
   Solution
      <form>
    1st Number : <input type="text" id="firstNumber" /><br>
    2nd Number: <input type="text" id="secondNumber" /><br>
    <input type="button" onClick="multiplyBy()" Value="Multiply" />
    <input type="button" onClick="divideBy()" Value="Divide" />
   
    <p>The Result is : <br>
    <span id = "result"></span>
    </p>

       function multiplyBy()
    {
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        document.getElementById("result").innerHTML = num1 * num2;
    }

        function divideBy()
    {
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        document.getElementById("result").innerHTML = num1 / num2;
    }

     
            Q . Write a JavaScript program to get the website URL (loading page).  
     solution
     console.log(document.URL)

  Q .   Write a JavaScript exercise to create a variable using
   a user-defined name.
     solution
    let name = 'abcd';
    let  n = 120;
    this[name] = n;
    console.log(this[name])

     Q . Write a JavaScript exercise to get the filename extension
     solution
     filename = "system.php"
     console.log(filename.split('.').pop());

     Q .   Write a JavaScript program to get the difference between
      a given number and 13, if the number is broader than 13 return
      double the absolute difference.
     solution
    function difference(n)
     {
    if (n <= 13)
        return 13 - n;
    else
        return (n - 13) * 2;
     }
    console.log(difference(32))
    console.log(difference(11))

     Q .  Write a JavaScript program to compute the sum of the two
      given integers. If the two values are the same,
   then return triple their sum.
   solution
  function add(a,b){
    if(a==b){
      return (a+b)*3 ;
    }
    else{
      return a+b ;
    }
   
      }
      ;
      console.log(add(2,3))
   

   Q . Write a JavaScript program to compute the absolute difference
    between a specified number and 19. Returns triple the absolute
    difference if the specified number is greater than 19.  
     solution
     let specificed num = n ;
      function ret(n){
        if(n>19){
          return 3*(n-19);
        }
        else{
         return (19-n);
        }

      }
      console.log(ret(20))
       

       Q .Write a JavaScript program to check a pair of numbers and
       return true if one of the numbers is 50 or if their sum is 50.  
        solution
        function check(a,b){
          if((a==50 || b==50) ||(a+b==50)){
            return true ;
          }
        }
        console.log(check(50,50))

                                    THANKS FOR VISTING HERE

Saturday, 3 June 2023

CodeQuest: The JavaScript Journey || A JavaScript Game

 


                           Create a Simple Game using javascript
 <!DOCTYPE html>
<html>
  <head>
    <title>Simple JavaScript Game</title>
    <style>
      #game-container {
        width: 400px;
        height: 400px;
        border: 1px solid black;
        position: relative;
      }
      #player {
        width: 50px;
        height: 50px;
        background-color: blue;
        position: absolute;
      }
    </style>
  </head>
  <body>
    <div id="game-container">
      <div id="player"></div>
    </div>
    <script >
            
// Get the player and game container elements
const player = document.getElementById("player");
const gameContainer = document.getElementById("game-container");

// Set the initial position of the player
let playerX = 0;
let playerY = 0;

// Function to handle keyboard arrow key events
function handleArrowKey(event) {
  const key = event.key;
  if (key === "ArrowUp" && playerY > 0) {
    playerY -= 10;
  } else if (key === "ArrowDown" && playerY < gameContainer.offsetHeight -
        player.offsetHeight) {
    playerY += 10;
  } else if (key === "ArrowLeft" && playerX > 0) {
    playerX -= 10;
  } else if (key === "ArrowRight" && playerX < gameContainer.offsetWidth -
        player.offsetWidth) {
    playerX += 10;
  }

  // Update the player's position
  player.style.left = playerX + "px";
  player.style.top = playerY + "px";
}

// Add event listener for arrow key events
document.addEventListener("keydown", handleArrowKey);

</script>
  </body>
</html>

This is me