• loops; simple to do list


    • while loop VS if loop:

       while loop, we don't know when the loop will stop, if loop, we know how many times the loop will run.

    • FOR......OF......
    let subarrays = ['hello', 'hi', 'how are you'];
    
    for (let sub of subarrays) {
        console.log(sub);
    }
    • Nested For...Of
    const SECRET = "HiBye";
    
    let guess = prompt("enter the secret code……");
    while(guess !== SECRET){
         guess = prompt("enter the secret code……");
    }
    console.log("Congrats you got the secret !!!")
    • To-do-list project
      • <!DOCTYPE html>
        <html lang="en">
        
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Todo List</title>
        </head>
        
        <body>
            <h1>Todo List</h1>
        
            <ul>
                <li>"new" -Add a Todo</li>
                <li>"list" - List All Todos</li>
                <li>"delete" - Remove Specific Todo</li>
                <li>"quit" - Quit App</li>
            </ul>
            <script src="todos.js"></script>
        </body>
        
        </html>
        let input = prompt("what would you like to do?");
        
        const todos = [];
        
        while (input !== "quit" && input !== "q") {
            if (input === "list") {
                console.log("**********")
                for (let i = 0; i < todos.length; i++) {
                    console.log(`${i}: ${todos[i]}`);
                }
                console.log("**********")
            } else if (input === "new") {
                const newTodo = prompt("ok, what is the new todo?");
                todos.push(newTodo);
                console.log(`${newTodo} added into the list`)
            } else if (input === "delete") {
                const index = parseInt(prompt("ok, enter an index to delete"));
                if (!Number.isNaN(index)) {
                    const deleted = todos.splice(index, 1);
                    console.log(`ok, deleted ${deleted[0]}`);
                } else {
                    console.log("Unknow index")
                }
            }
            input = prompt("what would you like to do?")
        }
        console.log("ok, quit the app")

  • 相关阅读:
    CF1454F Array Partition
    leetcode1883 准时抵达会议现场的最小跳过休息次数
    leetcode1871 跳跃游戏 VII
    leetcode1872 石子游戏VIII
    CF1355C Count Triangles
    CF1245D Shichikuji and Power Grid
    CF1368C Even Picture
    CF1368D AND, OR and square sum
    CF1395C Boboniu and Bit Operations
    SpringBoot和开发热部署
  • 原文地址:https://www.cnblogs.com/LilyLiya/p/14260393.html
Copyright © 2020-2023  润新知