Sunday, 4 June 2023

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

No comments:

Post a Comment

This is me