- 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")
-