Showing posts with label Js Practice Question. Show all posts
Showing posts with label Js Practice Question. Show all posts

Tuesday, 6 June 2023

5 Basic JavaScript Methods || Most Important Methods


 
         1.  Check if a variable is an array:

            Ans .   Array.isArray(variable);
 
         2 . Convert a string to lowercase:

            Ans.   var lowercaseString = string.toLowerCase();
 
         3 . Generate a random number between a minimum and maximum value:

        Ans . var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
 
         4. Get the current date and time:

          Ans . var currentDate = new Date();
 
        5.  Check if a string contains a specific substring:

        Ans .  var containsSubstring = string.includes(substring);


Monday, 5 June 2023

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'));

Friday, 2 June 2023

JavaScript Questions For Practice || JavaScript Practice || Important Question

 


JavaScript Questions For Practice || JavaScript Practice || Important Question || Practice Set

 
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  

// Solutions

// Make a Weeks Array
let weeks=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] ;
// For Get Current Date
let curdate = new Date() ;
// For Get Current Day
let days = weeks[curdate.getDay()] ;
// For Get Hour
let h = curdate.getHours();
// For Get  Minutes
let m = curdate.getMinutes();
// For Get Seconds  
let s = curdate.getSeconds();

// First Output
console.log("Today is : " + days) ;
// we use template literls also
console.log(`Today is : ${days}`) ;
// Second Output
console.log("Current time is " + h + " PM "+" : " + m +" : " + s );
// we use template literls also
console.log(`Current time is   ${h} PM  :  ${m}  : ${s}`);


   Q.2 Write a JavaScript program to print the current window contents ?
    
    solution :-

    <body>
    <p>Click the button to print the current page.</p>
    <button onclick="print_page()">Click For Print</button>
    </body>
    
     <script>
          function print_page()
        {
            window.print();
        }
     </script>

       
         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 :-

         <script>
        let d = new Date();
        let mm = d.getMonth();
        let dd = d.getDay();
        let yy = d.getFullYear();

        if (mm < 10) {
            mm = '0' + mm;
        }
        if (dd < 10) {
            dd = '0' + dd;
        }

        d = mm + '-' + dd + '-' + yy;
        console.log(d);

        d = mm + '/' + dd + '/' + yy;
        console.log(d);

        d = dd + '-' + mm + '-' + yy;
        console.log(d);

        d = dd + '/' + mm + '/' + yy;
        console.log(d);
        document.write(date)
    </script>

                                getcodr


This is me