My chrome browser won't show these codes on the console

59 views Asked by At

as soon as i run the codes on my chrome browser .. the CPU usage runs full 100%(weird though).

window.setTimeout(function() {
  
  var todos = ["Buy new Turtle"];
 
 var input = prompt("What would you like to do?");
 
 while(input !== "quit"){
     if(input === "list"){
         console.log(todos);
     } else if(input === "new"){
        var newTodo = prompt("Enter new Todo");
         todos.push(newTodo);
     }
 }
 console.log("OK. YOU HAVE SUCCESSFULLY LOGGED OUT! ");
       
    
 }, 500);
4

There are 4 answers

0
Hriday Sarma On BEST ANSWER

The code looks fine . Please check your file name if it is written correctly.

0
Arijit Jana On

You can try this:

window.setTimeout(function() {

  var todos = ["Buy new Turtle"];

 var inp = prompt("What would you like to do?");

 while(inp!=null){
     if(inp!="quit"){
     if(inp == "list"){
         console.log(todos);
     } else if(inp == "new"){
        var newTodo = prompt("Enter new Todo");
         todos.push(newTodo);
     }
    }
 }
 console.log("OK. YOU HAVE SUCCESSFULLY LOGGED OUT! ");


 }, 500);
0
tiagojpdias On

You should question again inside the while (or do/while) to grab the next action.

window.setTimeout(function() {
  var todos = [
    "Buy new Turtle",
  ];

  var input;

  do {
    input = prompt("What would you like to do?");
    
    if (input === "list") {
      console.log(todos);
    } else if (input === "new") {
      var newTodo = prompt("Enter new Todo");
      todos.push(newTodo);
    }
  } while (input !== "quit");
  
  console.log("OK. YOU HAVE SUCCESSFULLY LOGGED OUT! ");

}, 500);

0
Yogendra Chauhan On

Here is the working example:

var todos = ["Buy new Turtle"]; 
var input = prompt("What would you like to do?");
 
var interval = window.setInterval(function() {
  if(input === "quit"){
    window.clearInterval(interval);
    console.log("OK. YOU HAVE SUCCESSFULLY LOGGED OUT! ");
  } else {
     if(input === "list"){
         console.log(todos);
         window.clearInterval(interval);
     } else if(input === "new"){
        var newTodo = prompt("Enter new Todo");
        if(newTodo) {
          todos.push(newTodo);
        } else {
          input = prompt("What would you like to do?");
        }
     }
 }
 }, 500);