js knowledge consolidation

Consolidate the foundation:
    https://www.w3schools.cn/js/exercise_js.asp?filename=exercise_js_string_methods1
[String]
    1. Find the position of the character h in the string txt.
        var txt = "abcdefghijklm"; var pos = txt.
        indexOf("h");
        
    2. Use the slice method to return the word "bananas".
        var txt = "I can eat bananas all day"; var x = txt.slice(10, 17);
    
    3. Use the correct string method to replace the word "Hello" with the word "Welcome".
        var txt = "Hello World"; txt = txt.replace("Hello", "Welcome");
    
    4. Convert the value of txt to uppercase.
        var txt = "Hello World"; txt = txt.toUpperCase();
    
    5. Convert the txt value to lowercase.
        var txt = "Hello World"; txt = txt.
       
       


        var cars = ["Volvo", "Jeep", "Mercedes"]; cars[0]= "Ford";

    2. Use the correct Array property to remind you of the number of items in the array.
        var cars = ["Volvo", "Jeep", "Mercedes"]; alert(cars.length);
    
    3. Use the correct Array method to delete the last item of the fruits array.
        var fruits = ["Banana", "Orange", "Apple"]; fruits.pop();

    4. Use the correct Array method to add "Kiwi" to the fruits array.
        var fruits = ["Banana", "Orange", "Apple"]; fruits.push("Kiwi");
    
    5. Use the splice() method to delete "Orange" and "Apple" from fruits.
        var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.splice( 1,2 );
    
    6. Use the correct Array method to sort the fruits array in alphabetical order.
        var fruits = ["Banana", "Orange", "Apple", "Kiwi"]; fruits.sort(); [
        
        
Date]
    1. Create a Date object and remind you of the current date and time.
        var d = new Date(); alert(d);
        
    2. Use the correct Date method to extract the year (four digits) from the date object.
        var d = new Date(); year = d.getFullYear();

    3. Use the correct Date method to get the month (0-11) from the date object.
        var d = new Date(); month = d.getMonth();

    4. Use the correct Date method to set the year of the date object to 2020.
        var d = new Date(); d.setFullYear(2020);

【Math】
    1. Use correct mathematical methods to create a random number.
        var r = Math.random();
    
    2. Use correct mathematical methods to return the largest number between 10 and 20.
        var x = Math.max(10, 20);

    3. Use correct mathematical methods to round numbers to the nearest whole number.
        var x = Math.round(5.3);
    
    4. Use correct mathematical methods to find the square root of 9.
        var x = Math.sqrt(9);
    
 
[Comparison operator]
        1. When x is greater than y.
            x = 10; y = 5; alert(x > y);
        
        2. When x is equal to y.
            x = 10; y = 10; alert(x == y);
        
        3. When x is not equal to y, choose the correct comparison operator to alert true.
            x = 10; y = 5; alert(x != y);
        
        4. Select the correct conditional (ternary) operator. If the age is less than 18 years old, it will prompt "Too young", otherwise it will prompt "Old enough".
            var age = n; var voteable = (age < 18) ? "Too young" : "Old enough"; alert(voteable);


[js conditional statement]
        1. Fix the if statement to prompt "Hello World" when x is greater than y.
            if (x > y){alert("Hello World"); }
            
        2. Fix the if statement. If x is greater than y, it will prompt "Hello World", otherwise it will prompt "Goodbye".
            if(x >y) { alert("Hello World"); } else{ alert("Goodbye"); }
        
  
 
[Switch]
        1. Create a switch statement. If fruits is "banana", "Hello" will be prompted. If fruits is "apple", "Welcome" will be prompted. .
            switch(fruits) { case "Banana": alert("Hello") break; case "Apple": alert("Welcome") break; } 2.
            
        If fruits is neither "banana" nor "apple", add a The reminder ("Neither") part.
            switch(fruits) { case "Banana": alert("Hello") break; case "Apple": alert("Welcome") break; default:


【for loop】

        1. Create a loop from 0 to 9.
            var i; for(i= 0;i< 10; i++) { console.log(i); }
            
        2. Create a loop that iterates through each item in the fruits array.
            var fruits = ["Apple", "Banana", "Orange"]; for (x of fruits) { console.log(x); }

[while loop]
        1. Create a loop that runs as long as i is less than 10.
            var i = 0; while(i <10) { console.log(i); i++ }

        2. Create a loop that runs as long as i is less than 10, but increases i by 2 each time.
            var i = 0; while (i < 10) { console.log(i); i= i+2; }


[Break loop]
        1. Stop looping when i is 5.
            for (i = 0; i < 10; i++) { console.log(i); if (i == 5) { break; } }
        
        2. When i is 5, make the loop jump to the next iteration.
            for (i = 0; i < 10; i++) { if (i == 5) { continue; } console.log(i); }

Guess you like

Origin blog.csdn.net/lv_suri/article/details/131422496