• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
loops; simple to do list
To do list; loops
  • 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")

posted on 2021-01-11 03:52  LilyLiya  阅读(82)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3