Saturday, 3 June 2023

JavaScript Practice: Five Questions with Solutions || JS Practice

 

      
     Q.1  Write a JavaScript program to display the
      current day and time in the following format.  
      Sample Output : Today is : Tuesday.
      Current time is : 10 PM : 30 : 38
      Solution :-
      let days = ["Sunday","Monday","Tuesday","Wendays",
      "Thursday","Friday","Saturday",]
    let curDate = new Date() ;
    let  day = curDate.getDay();
    document.write("Today is : "+ days[day]) ;
    let H = curDate.getHours();
    let M = curDate.getMinutes();
    let S = curDate.getSeconds();
      document.write(`Current time is : ${H} PM : ${M} : ${S}`) ;
 

      Q.2 Write a JavaScript program to print the current
      window contents.
    <button onclick="window.print()">Click</button>


   Q.3 Write a JavaScript program to get the current date.  
        Expected Output :
        mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyy
     Solution
     let curDate = new Date();
      let mm = curDate.getMonth();
      let dd = curDate.getDay();
      let yyyy = curDate.getFullYear();
      if(dd<10){
        dd = "0"+dd ;
      }
      if(mm<10){
        mm = "0"+mm ;
      }
      document.write(`${mm}-${dd}-${yyyy} <br>`);
      document.write(`${mm}/${dd}/${yyyy} <br>`);
      document.write(`${dd}-${mm}-${yyyy} <br>`);
      document.write(`${dd}/${mm}/${yyyy} <br>`);
     


      Q.4 Write a JavaScript program to find the area of
         a triangle where three sides are 5, 6, 7.  
      Solution
     let a = 5 ;
     let b = 6 ;
     let c = 7 ;
      let s = (a + b + c)/2 ;
      let A = Math.sqrt(s*(s-a)*(s-b)*(s-c)) ;
      document.write(A) ;
       

        Q.5 Write a JavaScript program to determine whether a
           given year is a leap year in the Gregorian calendar.
        Solution
     
       function leapyear(year)
       {
        return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
      }
        console.log(leapyear(2016));
        console.log(leapyear(2000));
        console.log(leapyear(1700));
        console.log(leapyear(1800));

No comments:

Post a Comment

This is me