Is it possible to control the timing between console.log messages and user input prompts using JavaScript alone?

As I delve into learning JS, I am currently experimenting with a classic and simple JS "game" akin to a "Choose Your Own Adventure." Within this process, I am tackling fundamental "if/else" statements in order to achieve the desired output:

'{Prompt/getUserInput} first -- Then {console.log}, THEN {prompt}, then {console.log}, etc.'

Despite my efforts, when inputting the code into repl.it, what I end up with is {prompt}{prompt}{prompt} followed by all the console logs. For clarification, here is a snippet to illustrate my point:

var buffer = function(){
console.log("------------------");
}
console.log("You are alone in the woods...");

buffer()
var userInput = prompt("You can either walk further into the woods or turn around. Please enter 'go on' or 'screw this'");

if(userInput === "go on"){ 
console.log("It is getting darker and darker. By the time you hear his heavy breathing behind you, it is too late");
}

if (userInput === "screw this"){
console.log("You always knew you were the only person in the village with a lick of sense. Now, where did you hide those bodies...");
}

buffer();

Is there a way to transform the sequence from being read as AAA.BBB to a more preferable A.B.A.B pattern?

Answer №1

It seems like your code is correct, but there may be a bug with the repl.it platform itself. Try opening the console in your browser (Ctrl+Shift+J for Chrome or Ctrl+Shift+K for Firefox), paste your code, and hit enter to see if it works.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble accessing the value of null and adding it to an array

Having trouble with a 'value' of null error that is not getting resolved even after window.onload = function(){ Any suggestions on what to do? Here's the code snippet: window.onload = function(){ var shashank = document.getElementB ...

Choosing the next SELECT element with jQuery

JavaScript document.querySelectorAll('select[id^="_taskAssignedTo_"]').forEach(function(select) { if (select.value === "1") { select.nextElementSibling.querySelector('select[id^="_taskStatus_"]').disabled = false; } }); ...

Vuex fails to trigger updates on Vue computed properties

I have been integrating vuex as a middleware between firestore and my vue application. The structure of my vuex store is as follows: const store = new Vuex.Store({ state: { posts: [], }, mutations: { add_post(state, post) { ...

Mastering the art of tab scrolling in VueJS using Bootstrap-vue

Currently, I am working on a VueJS project that utilizes bootstrap-vue. The issue I am facing is that when rendering a list of tabs, it exceeds the width of its parent container. To address this problem, I aim to implement a solution involving a set of but ...

Tips for retrieving the dynamically generated ID within an li tag

I've been diving into the world of JavaScript and jQuery, encountering a few hurdles as I attempt to merge various solutions that I come across. This code represents a mishmash of tutorials I've recently completed. Admittedly, I am quite new to ...

Issue with loading Bootstrap modal in KnockoutJS

Currently, I am working on a project to develop a CRUD system using knockoutJS and fetching data through an AJAX call. While I am still in the process of implementing the add and delete functionalities, I am facing issues with my modal. The modal form does ...

Employing JavaScript to insert a JSON value as a parameter in an HTML element's attribute

In a JSON file, I have "img" properties with .jpg file paths as their values (e.g "images/image.jpg"). I am attempting to insert these values into the 'src' attribute of existing HTML elements. Below are my attempts so far: I tried creating te ...

Struggling to connect API from React and Node using a proxy server

I have encountered a troubleshooting issue with React and Node that I am struggling to resolve. The problem lies in the communication between my node server and coinmarketcap API. While I successfully receive data from both endpoints (all coins and individ ...

Removing a specific object from a MongoDB array by its unique identifier

I'm currently developing an application that focuses on playlists. Utilizing MongoDB with mongoose, I am storing videos within each playlist using an array structure as demonstrated below: { _id: ObjectId("61ca1d0ddb66b3c5ff93df9c"), name: "Playli ...

“What is the process of setting a referenced object to null?”

Here is an example of the code I'm working with: ngOnInit{ let p1 : Person = {}; console.log(p1); //Object { } this.setNull<Person>(p1); console.log(p1); //Object { } } private setNull<T>(obj : T){ obj = null; } My objective is to ...

How can I utilize an external file in Node js to output as a response?

I came across a similar inquiry, but I am interested in exploring manual methods. My goal is to achieve this without relying on express or any other external library. var http = require('http'); var server = http.createServer(function(req, res) ...

How can we allocate array elements dynamically within div elements to ensure that each row contains no more than N items using only HTML and JavaScript?

For instance, if N is 3 in each row, as more elements are added to the array, the number of rows will increase but each row will still have a maximum of 3 elements. I am currently using map to generate corresponding divs for every element in the arr ...

Ways to remove a JSON item based on its index position?

In my JSON object, I am trying to find a way to remove the first element. Here is an example of what I have: var obj1 = {property:'something', does:'somethingElse', is:'somethingCool'}; var obj2 = {does:'somethingElse&ap ...

When jQuery updates the content, the div content disappears temporarily

I am currently using jQuery to generate HTML by fetching data from a JSON file. Below is the code snippet where I create the HTML: $(document).ready(function () { $('#searchForm').submit(function () { var entry= $("#searchFo ...

Finding the x and y coordinates of a rotated 3D cube using JavaScript

In my quest to create a 3D cube using separate div elements, I am faced with a challenge. Imagine a cube made up of 3*3*3 divs where each div has: X, Y, Z coordinates in the 3D space Rotation angles around the X and Y axes With this information, I shoul ...

Creating repeatable texture patterns in Three.js

Hello, I have developed a basic renderer for my 3D objects that are generated using PHP. While I am able to successfully render all the objects, I am facing some major issues with textures. Currently, the texture I am using is sized at 512x512 pixels. I ...

Using AJAX to send an ID to another HTML page allows for the creation of a unique WHERE clause

UPDATED: MAY 17, 2017 9:50 AM I am currently working on two HTML pages that both utilize AJAX to fetch data upon page load. I now have a requirement where I need to pass an ID from one HTML page to another in order to retrieve specific data using a WHERE ...

Utilize Javascript/jQuery to play individual HTML audio files sequentially

On my website, each keyboard key is associated with an audio file. When a letter key from A to Z is pressed, the corresponding audio file is played. For all other keys (excluding ESC), a random audio file out of the 26 available is played. However, if mult ...

javascript code will continue running even after returning false

Below is the code I am using to validate password matching and make an AJAX call to check if the email exists. The issue that I am facing is that the code continues to execute after returning false from the function emailexist. The emailexist function retu ...

Utilizing Angular with Browserify

My goal is to use browserify for the following: var angular = require('angular'); angular.etc ... The Angular page on npmjs.org suggests using exposify, but I prefer sticking with the browserify method, where I can have a single bundle without ...