Want number value to change to string in javascript [closed] - javascript

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.

Related

Generate only one word in the list everyday at midnight without repetition in JavaScript [closed]

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 10 months ago.
Improve this question
May I ask if I have a list emotion=['happy','sad', 'hopeful', 'delighted', 'despite', 'satisfied', 'confused', 'bored', 'awe', 'curious'];
I want to generate only one word from the list at midnight only, without repetition (until it gets to the end of the list, then repeat again).
For example, if yesterday I get on the HTML webpage and got the word 'happy', then the next day I will have to get a word that is different from 'happy', could be 'sad', but when many days go by (here we have 10 words so 10 days) there is no more word in the list, then it can get back to the beginning of the list or so.
May I ask how could I do that in Javascript?
You could use the current date, along with the remainder operator (%) to get an incrementing, in-range index for each day. For example:
const emotions = ['happy','sad', 'hopeful', 'delighted', 'despite', 'satisfied', 'confused', 'bored', 'awe', 'curious'];
const dayNum = new Date().getDate();
const index = dayNum % emotions.length;
const todaysEmotion = emotions[index];
console.log(todaysEmotion);
Or, just:
emotions[new Date().getDate() % emotions.length];

How to calculate on which page a product should appear? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Let's say I have 72 products and each page can have up to 24 items. We know that we will have 3 pages thanks to this, but how can I calculate this mathematically? Let's say the input is 5 how do I calculate that product 5 is on page 1, and product 48 on page 2? Keep in mind that I have thousands of products and while the number of pages are irrelevant, each page may only have 24 items.
It's just maths. How many times the number of products per page can fit into the total product count.
I've assumed some variables with obvious names are around, but some JS code could look something like this:
let totalProductCount = 1234;
let productsPerPage = 24;
let wantedProduct=48;
//Total page count would be the number of times the products per page
//can fit into the total, rounded UP if not a whole number.
let totalPossiblePageCount =Math.ceil(totalProductCount/productsPerPage);
//so same calculation can give the page NUMBER for a given product number.
let pageForThisProduct = Math.ceil(wantedProduct/productsPerPage);
alert (`product ${wantedProduct} will be on page ${pageForThisProduct}/${totalPossiblePageCount}`);
This will say "product 48 will be on page 2/52", and changing the value of wantedProduct will change appropriately

JavaScript: Logic to display unknown number of tiles [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a situation where I get 4, 5 or 6 images/tiles.
Depending on the number of tiles, I need to format the images on the webpage.
Like this http://prntscr.com/9y75dw
If it's five images, I have to format it in such a way that two images in the first row and three images in the second row. Can you help me with the logic?
Well I don't see a technique, maybe I am missing to do that more appropriately or in a generic way but since the description in less and number of images given are random, I don't know how this will work.
var imageLength = $('img').length;
var newLength = 0, differenceLength=0;
if(imageLength%2==0){
//incase of even number
//Do what you like here eg: $('img').css('width', '50%');
}
else{
// incase of odd number
newLength = Math.round(imageLength/2); //dividing number into two parts.
differenceLength = imageLength - newLength; //difference to put smaller above and greater below.
$('parent-div img:nth-child(1)').nextUntil('img:nth-child('+differenceLength+')').wrapAll('<div></div>') //wraps into a container div
}
Although this is just one way. You might have already realized a lot of logic by now.
PS: I have randomly written this code so take it as a logic for help. Not sure whether this will work.

Javascript - Show all telephone numbers in a range [closed]

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.

Helping with JavaScript [closed]

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 9 years ago.
Improve this question
I would appreciate helping me and correcting my mistakes. :)
1- Consider the following code
Set the initial value of count to 12
While (NOT (count is 0))
Write out the value of count in the output window
Move to the next line in the output window
Reduce count by 3
Write ‘Finished’
Find the answer of following statement in the code above
A. find the condition
The condition is: NOT(count is 0).
B. find the loop body
The loop body consists of the three instructions:
Write out the value of count in the output window
Move to the next line in the output window
Reduce count by 3
C. find any instructions that are not in the loop body, and so are not repeated
There are two instructions outside the loop body:
Set the initial value of count to 12
Write ‘Finished’
D. How many times will the loop body be executed?
4 times
E. What will the output be?
Finished
F. Which instruction in the loop body ensures that the loop is not an infinite loop?
Reduce count by 3
Thank you in advanced..
Your answer to question E is not correct. The output should contain what is written in the while loop too.
and same in E , 12....3 finished

Categories