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

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.

Related

Want number value to change to string in 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 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.

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

Simple math coming out wrong [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I've made a super simple script to pop out some hourly rates from a pool of tips. Thing is, this one specific result always comes out wrong. What the heck is going on?
var tips = prompt('Enter final tips after payouts and cleaning');
//Hours worked for both positions
var tendHrsFirst = 11;
var tendHrsSecond = 10;
//Hourly Rate
var barThourly = ((tips/(tendHrsFirst+++tendHrsSecond)));
//This result here always comes out as if tendHrsFirst is 12 and not 11.
var barToneTotal = (tendHrsFirst * barThourly);
//This result is always correct
var barTtwoTotal = (tendHrsSecond * barThourly);
You are incrementing with tendHrsFirst++, so it actually is 12.
I guess those are actually two commands.
tendHrsFirst++ increments tendHrsFrist by 1. Afterwards, you add both numbers. Not sure why you think that's a good idea. Cleaning up your code should help avoiding such mistakes.
here
var barThourly = ((tips/(tendHrsFirst+++tendHrsSecond)));
you are using +++ that means postfix increase of tendHrsFirst and added to tendHrsSecond
or maybe
prefix increase of tendHrsSecond added to tendHrsFirst

jQuery count amount of divs on a page and hide? [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 6 years ago.
Improve this question
I'm displaying an amount of divs on a page that can be anywhere from 1-50, all will be generated and loaded into the HTML via PHP, but I want to only display 9 initially and then load an additional 9 on a button click until all are loaded.
var alldivs = $('.preview-container').hide();
$('button').on('click', function(){
var turn = alldivs.splice(0, 9);
if (turn.length) {
console.log(turn);
turn.fadeIn();
}
});
Your question is very vague. For a better reference, you need to post your current code, what precisely you need to do and what you have searched so far. So it can help you to receive a better answer. But most likely you are looking for something like this:
$('li').click(function() {
var which = $(this).index();
$('div').find('div').hide().eq(which).show();
});
This is the shortest code I can think to do this:
var alldivs = $('div'); // select the elements you want to show here
$('button').on('click', function(){
var turn = alldivs.splice(0, 9);
if (turn.length) {
turn.fadeIn();
}
});
As the jQuery-selector returns an array with the matched elements you can combine that with the Array splice method to do what you want.
Basically alldivs.splice(0, 9) remove nine items starting at position zero from alldivs and returns the removed items.
Hope it helps.

JavaScript check variable globally [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 8 years ago.
Improve this question
I want the global variable exp, when it's 100, to increase the level. I'm using a button to take exp.
It works fine. It gives exp and one other variable. But the level doesn't increment by 1 when exp is 100.
exp = 0;
level = 1;
250 lines of code later, I have this:
if (exp == 100) {
level = level + 1;
exp = 0;
document.getElementById("level").innerHTML = level;
document.getElementById("exp").innerHTML = exp;
}
It doesn't work. After 100, it keeps counting.
What am I doing wrong?
It's difficult to say what's wrong because you haven't included enough code here.
However, it appears that there are a couple of things that could be wrong:
Where have you defined exp and level? You haven't used var to define them above, which means that you have likely defined them somewhere else and are re-defining here, which would reset the count each time it increments, defeating the purpose of the incrementation.
What causes the incrementation to occur? Have you wrapped your if statement in the function you're calling to increment exp? If not, you may only be checking it the first time, which would cause it to never trigger the statement.
I've created a demo here that fills in some of these holes and it works fine in isolation. The question, of course, is how that piece fits within the context of your code. The solution to that requires a bit more information about how you have your project set up.

Categories