Showing posts with label Js Question & Answer. Show all posts
Showing posts with label Js Question & Answer. Show all posts

Thursday, 8 June 2023

Most Important JavaScript programms || JS Programms

 Most Important JavaScript programms

 

          Get the length of an array:
        var length = array.length;
       
        Convert an array to a string:
        var string = array.join(',');
       
        Check if a variable is null or undefined:
        if (variable == null) {
          // variable is null or undefined
        }
       
        Remove an element from an array by its index:
        array.splice(index, 1);
       
        Convert a number to a string:
        var string = number.toString();
       
        Get the current URL of the page:
        var currentURL = window.location.href;
       
        Round a number to a specified decimal place:
        var roundedNumber = number.toFixed(decimalPlaces);
       
        Get the index of the first occurrence of an element in an array:
        var index = array.indexOf(element);
       
        Create a new object with specific properties from an existing object:
        var newObject = {
          property1: existingObject.property1,
          property2: existingObject.property2
        };
       
       

Wednesday, 7 June 2023

Most Important JavaScript Methods || JavaScript Practice

 JavaScript Methods 

 

             1. Remove all whitespace from a string:

             Ans:-

            var stringWithoutWhitespace = string.replace(/\s/g, '');

           2. Convert a string to an integer:

           Ans:-

            var integer = parseInt(string);

            3. Sort an array in ascending order:

             Ans:-

            array.sort(function(a, b) {
              return a - b;
            });

            4. Find the maximum value in an array:

            Ans:-

            var max = Math.max(...array);

            5. Find the minimum value in an array:

            Ans:-

            var min = Math.min(...array);


                                  Thanks for visiting



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