This question already has answers here:
"Variable" variables in JavaScript
(8 answers)
Closed last month.
So, I am working on a wheel of fortune.
I have a const list with a lot of questions.
Underneath the const list is a randomizer that picks a random question.
The thing is that on the wheel of fortune segments it displays the random question but I want to show the const name.
After it stops spinning it indicates the segment and make an alert with the question.
const questioncard = ['question1','question2','question3']
const randomquestioncard = questioncard[Math.floor(questioncard.length * Math.random())];
Why don't you keep the data like this:
[{question:'question1', text:'text of the question 1'},{question:'question2', text:'text of the question 2'},{question:'question3', text:'text of the question 2'}]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm working on a monopoly game in javascript. I am fairly new to javascript so please forgive if I don't state things in a logical way. Anyways, I have a position player counter which tracks the space each player is at( there are 40 spaces total within the game ) . I also have a counter which counts a dice roll and resets every turn ( 1 to 6 ) .Right now the position player counter is just a number from 1 to 40 and obviously this is no good for the user experience and I want this number to represent the name of the space ( park place, marvin gardens, or whatever the name of the space happens to be ) .
I think I have the right idea of the player location being linked to a number between 1 to 40 because logically that makes the most sense to me, but I want to convert this counter number to the name of the space within the game. Obviously this should also update after every dice roll. I'm including a picture to show what I mean. The thing I'm trying to change is in the upper right. monopoly game
Once again, I'm sorry that my descriptions may be crude. I have only been studying web design and javascript for a few months and this is my first beginner project and my first post here. Thanks everyone.
You can use an array to map the numbers to the names:
var spaceNames = ["Space 1", "Space 2", "Space 3", "Space 4"];
Then you can get the names like this:
spaceNames[spaceNumber - 1];
The -1 is needed because Javascript arrays are 0 based, meaning that the first item in an array is 0. You can read more about that here.
This question already has answers here:
Saving a Javascript variable for later usage?
(4 answers)
Persist variables between page loads
(4 answers)
Closed 3 years ago.
I am trying to make a simple game in browser without using any backend. Is there a way for me to store the highest score in the browser using only javascript or react?
Yes , of course . You can use localStorage , this will alow you to store any information in the browser and fetch it later.
Example :
let score = 0;
score = 50;
localStorage.setItem('currentScore', score);
// And if you want to get the score
let getCureentScore = localStorage.getItem('score');
// do something with getCurrentScore
Hope this could helps you :)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.
Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.
This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 9 years ago.
I am new to JavaScript so would appreciate help on this issue I have.
I have a booking form I have made and I have 3 Eur results that come up automatically following the selection choices made (#Martina helped me with this challenge).
What I would like to do is get the results to show with 2 decimal places only - here is the link to the page I am talking about:
https://www.alpinemalta.net/oilandgaslibya/bookNow.html
I've tried looking through some of the posts in this forum and attempted some of the things but my newness to JS has not helped :(
Thanks again in advance for any help on this.
Cheers - Chris Brown
The Number class in Javascript has a .toFixed method that does exactly what you require, e.g.:
var n = 234.1
var s = n.toFixed(2); // s = "234.10"
I have the following test app: http://dev.driz.co.uk/quiztest/
The idea is that a series of webpages will each have a div that has the section number of quiz page e.g. 1 and then pull out the questions from a JSON file for that section.
This part I have working thanks to help from the SO community, so for clarity I have created a new question to deal with these issues which are separate.
The problem I have is that I only want the FIRST question to appear from a section on page load, and then when the user clicks on either the A or B button it then needs to load in the next question, so show question 2, and then 3 and so on...
Only one question at a time will be in the #questions ul for the user to answer.
Once all the questions for a section have been displayed I then need to do a callback to the finished function so that I can do some extra logic.
How do I best go about doing this? I'm thinking I'd need to first count the number of questions in the section and then do an increment to find the end. Can anyone help?
Edit: I'm planning on doing something like: Question 3 / 10 so getting that number is quite important it would seem. But I'm confused about getting the next question after the first and so and so on until then end of the section...
Thanks
I mocked what that website is in a fiddle. I don't know if this is exactly what you are looking for. Maybe with more explanation I could get it closer. Fiddle
Let's assume you have an array that contains your questions. Each question is an object, with a question key and an answers key. The question will always be a string that contains the text for the question. The answers will be an array with 0 or more elements of any type which signify the selectable answers to the question.
var questions = [
{question : 'What is 2 + 2?', answers : [4, 5, 6]},
{question : 'What is 6 * 4?', answers : [20, 24, 28]}
];
Now let's write a function to display a question. We'll use a global variable that stores the index of the question to be displayed (initialised to 0 to show the first question):
var currentQuestion = 0;
function displayQuestion() {
if(currentQuestion < questions.length) { // if there's still questions to display
var question = questions[currentQuestion]; // get the question to display
var questionHtml = ...; // generate the HTML for the question here
$('#questions').html(questionHtml);
currentQuestion += 1; // increase tracking index
}
else {
finished();
}
}
Then all you need to do is bind click event handlers to your buttons to call that displayQuestion() function.