Updating new values with the function is not successful

I'm fairly new to JavaScript and I've encountered a problem. The issue is that the prompt in my code doesn't update with the correct values, while the 'console.log' function does for some reason. I'm really curious as to why this happens and if there's a way to make the prompt update like the 'console.log' function does. I've attempted to rearrange the positions of the variables in the code, but since I'm not an expert, I'm unsure if the variable positions were ever correct.

Any help would be greatly appreciated. Please let me know if anything is unclear!

var alive = true;    
var destinationArray = ["town ", "areas ", "bosses"];
var destinationArraySet = 1;
var allDestination = destinationArray.slice(0, destinationArraySet).join(' ').trim();
var userDestinationPrompt = ("Where would you like to go? Available places: \n" + allDestination +".");

var userDestination = function () {
    allDestination = destinationArray.slice(0, destinationArraySet).join(' ').trim();
    console.log(allDestination);
    userDestinationAnswer = prompt(userDestinationPrompt).toUpperCase();
    destinationArraySet++;
};

while (alive) {
    userDestination();
}

Answer №1

"userLocation" is a newly created method.

The variable "userLocationPrompt" has already been assigned a value before calling the method.

Updating the value within the method should resolve your problem.

var userLocation = function () {
    var allLocations = locationArray.slice(0, locationNumber).join(' ').trim();
    console.log(allLocations);
    var userLocationPrompt = ("Where are you located? Available locations: \n" + allLocations + ".");
    userLocationAnswer = prompt(userLocationPrompt).toUpperCase();
    locationNumber++;
};

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

Developing a node.js application with express that effectively manages synchronous function calls and value passing in a modularized structure

I am currently developing a node.js/express application to validate user account details in order to determine their eligibility to access a specific resource. Initially, all the code was housed in a single app.js file, but now I am restructuring it into m ...

Instructions for setting a default value for ng-options when dealing with nested JSON data in AngularJS

I need help setting a default value for my ng-options using ng-init and ng-model with dependent dropdowns. Here is an example of my code: <select id="country" ng-model="statessource" ng-disabled="!type2" ng-options="country for (country, states) in c ...

Executing Jquery ajax based on specific key inputs

I've developed a script that transmits data from an input to a script every time a keyup event occurs. The issue is that it sends a request for every key press, even unnecessary ones like arrow keys. Is there a way to prevent these unnecessary request ...

How can I manipulate image resolution and export images of varying sizes using Javascript?

I am new to using three js. I have successfully exported an image in png format using the WebGL renderer. However, when attempting to export the image in a different custom size resolution, the resolution does not change. How can I set the image resoluti ...

Steps for sending data to a modal window

Consider this scenario: I have a function that retrieves some ids, and I utilize the following code to delete the corresponding posts: onClick={() => { Delete(idvalue[i]) }} Nevertheless, I am in need of a modal confirmation before proceeding with the ...

Accessing a variable across multiple windows in Chrome DevTools

How can I access a variable across multiple windows? I am looking to utilize the Chrome Devtools console from various domains. Thank you in advance! https://i.sstatic.net/GJjE4.png ...

Transform JSON object array into a different format

Can anyone help me with an issue I am facing with checkboxes and arrays in Angular 2? I have checkboxes that capture the value "role". Each role is stored in an array called "selectedRoles". However, when I try to console.log this.selectedRoles, I get str ...

Assign a CSS class when the page is loaded

I have the following HTML code: <body class="page-header-fixed page-sidebar-closed-hide-logo page-content-white page-sidebar-closed" style="background-color: #F5F5F5"> When the page loads, the sidebar closed CSS will be applied. Currently, I am us ...

Why doesn't TypeScript perform type checking on a two-dimensional array?

Here's a simple code snippet I've been using to create a 2D array: type cell = { id: string; }; const board: cell[][]; board = Array(10) .fill("") .map((x) => Array(10).fill(ANY TYPE CAN GO HERE WHY?)); Oddly enough, when I popu ...

Prevent navigation bar from appearing within the top 2em of the viewport

My website has a navigation bar that begins at the bottom left of the page. I would like it to pause 2em from the top of the viewport when scrolling up. <script> var mn = $(".main-nav"); mns = "main-nav-scrolled"; $(window).scroll(function() ...

Serialization of select value in Ajax/PHP form submission

Currently, the form is sending the correct information but it is missing the state field. The issue arises because the value of the state field is not fixed and depends on the selected state. How can I address this? JS: $('#sendFormBtn').live(& ...

Upcoming verification with JSON Web Token

I am looking to incorporate JWT auth into my Next app. Currently, I have mapped out the flow as such: User enters email and password to log in Server responds with status 200 and a jwt access token in httpOnly cookies My main dilemma lies in deciding on ...

The JSX function fails to display the return value

Having trouble getting the return value of a function to display on screen. The object words.First.wordsdata contains key-value pairs. import React from "react"; const WordList = ({ words }) => { return ( <div> ...

Issue with loop detected in <fieldset> tags - prematurely adding closing tag

In the development of my web app using Google Apps Script, I am faced with the challenge of creating a set of checkbox fields for each learner/student, displayed in rows of three. These checkboxes are generated from data stored in a spreadsheet. My goal i ...

Having trouble with creating SQLite tables using JavaScript within a for loop

I have developed a multi-platform app using AngularJS, JavaScript, Phonegap/Cordova, Monaca, and Onsen UI. In order to enable offline usage of the app, I have integrated an SQLite Database to store various data. After conducting some basic tests, I confir ...

Most effective method for grouping array elements by multiple criteria in JavaScript using ES6 syntax

Hello developers, I have a question about how to categorize an array of objects with different values into specific sub-groups based on certain criteria. Each subgroup should contain objects with specific values according to the queried key. Here is an ex ...

Struggling with handling various active states in ReactJS?

Good day to all my fellow stackOverflowers! I'm looking for advice on how to maintain the independence of active states when clicked. I'm struggling to prevent them from affecting each other. Each button starts with an active status of false by d ...

When using json_decode(), it will return a null value instead of an array

I am trying to upload an array of objects into a MySQL database using AJAX, but when I use json_decode() on the server side, it returns null. What is the solution to this issue? Here are the AJAX codes: let mainObj = [ { username: 'david', ...

Locating the declaration for my TypeScript/React module: Where to find it?

As someone who is completely new to frontend technologies, particularly react and typescript, I encountered an issue while attempting to use a react component from https://github.com/ckeditor/ckeditor5 I came across this example in the documentation: htt ...

Switching the Date Property of a Mongo Document using Just One Query

Is there a simple way to update a single document in Mongo using just the _id? I believe the answer is "no", but I'm curious to know if there is a method that allows for this. Target a document by _id (single document). If the readAt field exists on ...