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

Saturday, 17 June 2023

How to Make a Bulb Effect Using JavaScript || JavaScript Projects


   
              <!DOCTYPE html>
             <html lang="en">

            <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>light-blub </title>
   
            </head>

            <body>
   
            <button id="turnon" onclick="document.getElementById('bulb')
           .src='/light-blub/img/on-bulb.jpg' ">Turn on</button>
            <img id="bulb" src="/light-blub/img/off-bulb.jpg" alt="" class="center">
            <button id="turnoff"  onclick="document.getElementById('bulb')
            .src = '/light-blub/img/off-bulb.jpg' ">Turn Off</button>
            </body>
            </html>


Monday, 12 June 2023

Mostely Used Math Methods In JavaScript || JS Methods

 

In JavaScript, there are numerous mathematical methods available. Here are five commonly used math methods:


1. Math.abs(x): This method returns the absolute (positive) value of a number. For example:


            Math.abs(-5); // returns 5

2. Math.round(x): It rounds a number to the nearest integer. If the decimal part is 0.5 or higher, it rounds up; otherwise, it rounds down. For example:


        Math.round(3.6); // returns 4

3. Math.ceil(x): This method rounds a number up to the nearest integer, regardless of the decimal part. For example:


        Math.ceil(2.3); // returns 3

4. Math.floor(x): It rounds a number down to the nearest integer, regardless of the decimal part. For example:


        Math.floor(4.8); // returns 4

5.Math.random(): This method generates a random floating-point number between 0 and 1 (exclusive). For example, to generate a random integer between 1 and 10, you can use the following formula:


        Math.floor(Math.random() * 10) + 1;

These are just a few examples of commonly used math methods in JavaScript. There are many more available in the `Math` object that provide various functionalities for mathematical operations.


Explain :- 

Certainly! Here are some labels for the most commonly used math methods in JavaScript:


1. Absolute Value:

   - Method: `Math.abs()`

   - Purpose: Returns the absolute (positive) value of a number.


2. Rounding:

   - Method: `Math.round()`

   - Purpose: Rounds a number to the nearest integer.


3. Ceiling:

   - Method: `Math.ceil()`

   - Purpose: Rounds a number up to the nearest integer.


4. Floor:

   - Method: `Math.floor()`

   - Purpose: Rounds a number down to the nearest integer.


5. Random Number:

   - Method: `Math.random()`

   - Purpose: Generates a random floating-point number between 0 and 1.


These labels provide a brief description of the purpose of each method, helping to identify their primary functionalities.

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



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