cleaver Programmers JavaScript with profit

 JavaScript with profit

 

 index.html file

 
    <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>Document</title>
    </head>
    <body>

        <h1 id="someText"><h1>

        <script src="home.js"></script>
    </body>
    </html>
 

 

home.js file


    alert('this is sayed');
    console.log("Hello World");


    var a = "hey dear";
    console.log(a);

    var b = 3423;
    console.log(b);


    let age = prompt("what is your age? ")
    document.getElementById('someText').innerHTML = "your age is: "+ age;



Function in JS

 
    function fun() {
        alert("this is a function.");
        console.log("This is a function 2")
    };

    fun();
 

 


    function greeting(){
        let name = prompt("what is your name?");
        result = "Hello! "+ name;
        console.log(result);
       
    }
    greeting();
 

 

 
    // how do arguments work in functions
    // How do we add 2 numbers together in a function?

    function sumNumbers(num1, num2) {
        let result = num1 + num2;
        console.log(result);
    }
    sumNumbers(22,33);
 

 

 
    function greeting(yourName){
        let result =  "Hello " + yourName;
        console.log(result);
    }

    let name = prompt("what is your name?");
    greeting(name);


 

While loops


    // while loops
    var num = 0;

    while (num < 100){
        num += 1;
        console.log(num);
    }

 

For loops


    // For loops
    for(let num = 0; num < 100; num++){
        console.log(num);
    }

 

 

Data types in JS


    // Datatypes
    let yourAge = 18;   // number
    let yourName = "Bob Marley";    // string
    let name = {first: "Jo", last:"Job Current"};   // object
    let truthh = false; // boolean
    let fruits = ["Apple", "Banana","Graps"];   // array
    let random;     // undefined
    let nothing = null;     //  value null

 

Strings in JavaScript (common method)

 
    // String in JavaScript (common methods)
    let fruit = "Banana";
    let moreFruits = "Bananana\nApple";
    console.log(fruit.length);
    console.log(fruit.indexOf("nan"));
 

 

 Array in JS



    // Array
    let fruits  = ["Banana", "Apple","Orange","Pine Apple"];
    // Another way to create an array
    let cities = new Array("Japan","Malaysia", "Singapore","Hong Kong");

    // alert(fruits[3]);

    // fruits[3] = "Peer";
    // console.log(fruits);


    // for(let i = 0; i < fruits.length; i++){
    //     console.log(fruits[i]);
    // }


    // // Array common methods
    // console.log("to string: " ,fruits.toString());

    // console.log(fruits.join("-"))
    // console.log(fruits.join("*"))
    // console.log(fruits);
    // console.log(fruits, fruits.pop(), fruits);
    // console.log(fruits.push("Blackberry"),fruits);
    // console.log(fruits.push("Pomogrant"));
    // console.log(fruits,fruits.shift());
    // console.log(fruits.shift() + "\nAfter removed\n"+ fruits);


    let vegetable = ['Asparagus',"Tomato","Broccoli"];
    let allVegetable = fruits.concat(vegetable);    // combine array
    // console.log(allVegetable);
    // slicing array
    // console.log(allVegetable.slice(1,4));
    // Reverse array
    // console.log(allVegetable.reverse());
    console.log(allVegetable.sort());

    let number = [2,4,5,64,3,44,36,1,9,8];
    // console.log(number.sort());
    console.log(number.sort(function(a,b){return a-b}))     // sorted in accending order
    console.log(number.sort(function(a,b){return b-a}))      // sorted in deccending order


    let emptyArray = new Array();
    for(let num= 0; num <= 10; num++){
        emptyArray.push(num)
    }
    console.log(emptyArray);


 

 Objects in JS

 
    //  Objects in Javascript
    // Dictonaries in Python

    let student = {
        first: "Rafeh",
        Last: "Qazi",
        age: 23,
        height:170,
        studentInfo: function (){
            return this.first + "\n" + this.Last + "\n" +this.age;
        }
    };

    console.log(student);
    console.log(student.Last);
    console.log(student.first);
    student.first = "No Rafeh"; // change value
    console.log(student.first);
    student.age+=3;
    console.log(student.age);
    console.log(student.studentInfo());



 

 

 

 

 

JSON file 

    [
        {
            "name": "Cyed",
            "age": 26,
            "height": 170
        },
        {
            "name": "Chauli",
            "age": 98,
            "height": 150
        }
    ]

 
        <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>Document</title>
        </head>
        <body>

            <script>
                let student = `[
        {
            "name": "Cyed",
            "age": 26,
            "height": 170
        },
        {
            "name": "Chauli",
            "age": 98,
            "height": 150
        }
    ]`

    console.log(student);
    console.log(JSON.parse(student));
    console.log(JSON.parse(student)[0]);
    console.log(JSON.parse(student)[0].name);
    console.log(JSON.parse(student)[1].age);
    </script>


    </body>
    </html>
 
 
    output:
 
    [      {     "name": "Cyed",     "age": 26,      "height": 170      },      {      "name": "Chauli",      "age": 98,      "height": 150      }
    Array [ {}, {} ]
    length: 2

    Object { name: "Cyed", age: 26, height: 170 }
    age: 26
    height: 170
    name: "Cyed"

    Cyed
    98
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Post a Comment (0)
Previous Post Next Post