Learn JS

 Learn JS

 

Array

 
    // array

    let selectedColors = ["Red","Blue"];
    console.log(selectedColors);


    // indexing array element.
    console.log(selectedColors[0]);
 
 
    output:
    [ 'Red', 'Blue' ]
    Red
 

 

JavaScript provides eight different data types which are  

undefined,

 null,

 boolean,

 string,

 symbol,

 bigint,

 number, and

 object.

 

 Primitive Data type


    // These are primitive data type
    let name = "HelpMe"     // string Literal
    let age = 30;       //number Literal
    let isApproved = true;      // boolean
    let fName = undefined;
    let lastname = null;


 

 

Reference Data type


// //  Reference data type
// object      ---->its a real life like name, address, property and more
// Array
// Function
 

 

 When u concatenate Undefined with number


    console.log(undefined + 10);
 
    output: NaN



NaN - Not a Number.


When u concatenate Undefined with String

   
 console.log(undefined + "10");
    
 output: undefined10
            






While Loop

 



 

    let num = 1;        // initialization
    while(num <=10){    // condition      
        console.log(num);
        num++;              // update step
    }


    
    output:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

 

 

 

 

 

 

For Loop

 




    for(let num=1; num <=5; num++){
    console.log(num);
    }
 
    output:
    
    1
    2
    3
    4
    5




Array


    let arr = ["life",1,"22",['1',"Kiran"], 34];
    console.log(arr);

    output:
    [ 'life', 1, '22', [ '1', 'Kiran' ], 34 ]




    let arr = ["system",1,"22",['1',"Kiran"],34];
    console.log(arr[0]);
    
    output: system
    



    let arr = ["system",1,"22",['1',"Die hard"],34];
    console.log(arr[3][1]);
 
    output: Die hard 




Print Array using for loop

 
    
    let arr = ["system",1,"22",['1',"Die hard"],34];
 
    for(let i = 0; i<=arr.length; i++){
        console.log(arr[i]);
    }
  
    output:
    
    system
    1
    22
    [ '1', 'Die hard' ]
    34
    undefined
 



Questions:

We have an array of 0's and 1's.  Our task is to put all the zeroes at the start of the array
and all the 1's at the end of the array.

input: [1,0,1,1,0,0,0]
output: [0,0,0,0,1,1,1]
 

Solution:

 
    let arr = [1,0,1,1,0,0,0];
    let count0 = 0;
    let count1 = 0;

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == 0){
            count0++;
        }
        else{
            count1++;
        }
    }
    console.log(count0, count1);
    for(let i =0; i < arr.length; i++){
        if(count0 > 0){
            arr[i] = 0;
            count0 --;
        }
        else{
            arr[i] = 1;
        }
    }

    console.log(arr);
 
     
   output:   
  
    4 3
    [
      0, 0, 0, 0,
      1, 1, 1
    ]


 another solution

 
    let arr = [1,0,1,0,0,1,0];
    console.log(arr.sort());
 
 
    output:
    
    [
      0, 0, 0, 0,
      1, 1, 1
    ]
 
 
    let arr = [1,0,1,0,0,1,0];
    arr.sort();
    console.log(arr);
 
    output:
    
    [
      0, 0, 0, 0,
      1, 1, 1
    ]
 
 
 

 another solution

 

 
    let arr = [0, 1, 0, 1, 1, 0];
    let left = 0;
    let right = arr.length -1;

    while(left <= right){
        if(arr[left] == 0){
            left++;
        }
        else{
            // swap
            let temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            right--;
        }
    }

    console.log(arr);
 
 
    output:
    
    [ 0, 0, 0, 1, 1, 1 ]
 
 

 


Let's create a program to 1,1 1,2 1,3 and 2,1 2,2 2,3 and 3,1 3,2 3,3.
skip 1,1 2,2 3,3.

continue jam statement

 
    let n =3;
    for(let i=1; i<=n; i++){
        console.log(i);
        for(let j =1; j<=n; j++){
            if(i ==j){
                continue;    // continue is a jam statement
            }
            console.log(i,j);
        }
        console.log("=========================")
    }
 
     OUTPUT:
    
    1
    1 2
    1 3
    =========================
    2
    2 1
    2 3
    =========================
    3
    3 1
    3 2
    =========================
 
 
 
 


Another jam statement 'break'

 

 
    let n =3;
    for(let i=1; i<=n; i++){
        console.log(i);
        for(let j =1; j<=n; j++){
            if(i ==j){
                break;
            }
            console.log(i,j);
        }
        console.log("=========================")
    }
 
 
    OUTPUT:
    1
    =========================
    2
    2 1
    =========================
    3
    3 1
    3 2
    =========================
 



Ternary Operator have 3 operands.

 
    let mark = 50;
    let result = mark >= 40 ? "pass":"fail";
    console.log(result);
 
    OUTPUT: pass 


    let rain = false;
    let goOutside = rain == true?  "don't go outside":"go outside";
    console.log(goOutside);
 
      OUTPUT: go outside




 
    let mark = 70;
    let result = mark >= 40? mark >=70 ? "Excellent": "pass":"fail";
    console.log(result);
    
      OUTPUT: Excellent
 



 
    let raining = false;
    let umbrella = true;
    let goOut = raining == true? umbrella == true? "Go outSide": "Stay at home":"go out";
    console.log(goOut);
 
    OUTPUT: go out 
 



Switch Case



    // switch case

    let weekDayNum = 5;
    let weekDay;
    switch(true){
        case(weekDayNum ==1):
            weekDay = "Monday";
            break;
        case(weekDayNum ==2):
            weekDay = "Tuesday";
            break;
        case(weekDayNum ==3):
            weekDay = "Wednessday";
            break;
        case(weekDayNum ==4):
            weekDay = "Thursday";
            break;
        case(weekDayNum ==5):
            weekDay = "Friday";
            break;
        case(weekDayNum ==6):
            weekDay = "Satureday";
            break;
        case(weekDayNum ==7):
            weekDay = "Sunday";
            break;
           
        // default:
        //     weekDay = "Any Day";

    }
    console.log(weekDay)
 
    OUTPUT: Friday 
 
 


Switch case is same to if-else statement.



Questions:

Create a program to find if the number is positive, negative or
zero.

 
    let num = -2;
    switch(true){  
        case(num > 0):
        console.log("Positive");
        break;
        case(num < 0):
        console.log("Negative");
        break;

    default:
        console.log("Zero");
    }

    OUTPUT: Negative




print like below star

 
 *
 *  *
 *  *  *
 *  *  *  *
 *  *  *  *  * 
 

Solution


    let n =5;
    for(let row = 1; row <= n; row++){
        // console.log(row);
        let star = "";
        for(let starCount =1; starCount <= row; starCount++){
        star = star + " * ";
        }
        console.log(star)

    }
 
 
    OUTPUT:
 
     *
     *  *
     *  *  *
     *  *  *  *
     *  *  *  *  *
 
 
 

 

 

Armstrong number

 
    let numOriginal = 371;
    let num = numOriginal;
   
    let numDigits =0;
    while( num > 0){
    let lastDigit = num%10;
    numDigits++;
    num = parseInt(num/10);
    }
    console.log(numDigits)
   
    let sum = 0;
    num = numOriginal
    while( num > 0){
    let lastDigit = num%10;
    sum = sum + Math.pow(lastDigit, numDigits);
    num = parseInt(num/10);
    }
    console.log(sum);
    let result = sum == numOriginal? "armstrong Number" : "not an armstrong Number";
    console.log(result);
 



Print a program like  below

   *
  **
 ***
****



    let n = 4;
    let pattern = "";
    for(let row = 1; row <=n; row++){
        // console.log(row);
        // print space
        for(let space =1; space <= n-row; space++){
            pattern = pattern + " ";
        }
        // print star
        for(let star =1; star <= row; star++){
            pattern = pattern + "*";
        }
        pattern = pattern + '\n';
    }
    console.log(pattern);
 
 
 
     output:
 
       *
      **
     ***
    ****





Print a program like  below

    *
   *  *
  *  *  *
 *  *  *  *


 
    let n = 4;
    let pattern = "";
    for(let row = 1; row <=n; row++){
        // console.log(row);
        // print space
        for(let space =1; space <= n-row; space++){
            pattern = pattern + " ";
        }
        // print star
        for(let star =1; star <= row; star++){
            pattern = pattern + " * ";
        }
        pattern = pattern + '\n';
    }
    console.log(pattern);
 
 
    output:
 
    
        *
       *  *
      *  *  *
     *  *  *  *
      



Print a program like  below

 *
 *  *
 *  *  *
 *  *  *  *




    let n = 4;
    let pattern = "";
    for(let row = 1; row <=n; row++){

        for(let star =1; star <= row; star++){
            pattern = pattern + " * ";
        }
        pattern = pattern + '\n';
    }
    console.log(pattern);


    Output:
    
     *
     *  *
     *  *  *
     *  *  *  *




Print a program like  below

    *
    * *
    * * *
    * * * *
    * * *
    * *
    *




    let n = 4;
    let pattern = "";
    for(let row = 1; row <=n; row++){
        console.log(row);

        for(let star =1; star <= row; star++){
            pattern = pattern + "* ";
        }
        pattern = pattern + '\n';
    }
    // second half trinagle
    for(let row = n-1; row >= 1; row--){
        console.log(row);
        // print stars
        for(let star = 1; star <= row; star++){
            pattern = pattern + "* ";
        }
        pattern = pattern + '\n';
    }

    console.log(pattern);
 
 
 
 
    output:
    
    *
    * *
    * * *
    * * * *
    * * *
    * *
    *
 








7 MARCH TOPICS

 
    console.log("Hi");
    setTimeout(function(){
        console.log("click the button");
    }, 1000);

    console.log("Welcome to loop")
 
 
    OUTPUT:    
    Hi
    Welcome to loop
    click the button
 
 



















إرسال تعليق (0)
أحدث أقدم