Console.log prints after all code is executed

I’m working on a JS program with my students that will print to the console, but I need to print to the console immediately after inputs from prompts instead of waiting until all code has executed. For example, in the code below I want to print firstName as soon as it is input, but nothing gets printed to the console until after both prompts. Is what I want possible on repl.it?

let firstName = prompt('What is your first name?');
console.log(firstName);

let lastName = prompt('What is your last name?');
console.log(lastName);```
1 Like

Hi @JJMichael could you please post a link to the Repl here so the community can suggest some ideas?

Hey!

console.log() and prompt() are made for two different interfaces, so they don’t work well together. However, I think you need to add a delay to it like this:

let firstName = prompt('What is your first name?');
console.log(firstName);

// Force a 10 millisecond delay before running the rest of the code.
setTimeout(function(){
  let lastName = prompt('What is your last name?');
  console.log(lastName);
}, 10);

Here is the link to the repl: https://replit.com/@KruJJMichael/Battleship?v=1

I actually got it working the way I want it to. In the normal repl window nothing prints to the console until the game is finished. But if I open it in a new tab it prints to the console after each prompt input.

Hi @JJMichael I forked your repl to test this out and I see what you mean. When using the Replit UI the console doesn’t update until the program has finished running.

I’ll log this as a potential bug with Replit Support

Thanks @IanAtReplit!

1 Like