Javascript using the ceiling function for value less than 1 - javascript

So, I have a list of items and I want to check how many pages I would need if I want to show , say, 10 items per page
ie
220 should give me 22 pages
134 should give me 14 pages
I am using the ceiling function to get the number of pages
var pages = parseInt(items)/10;
alert("pages " +pages);
alert(Math.ceil(pages));
The problem which I am having is if I have 7 items, I am expecting it to give me 1 page. However, I don't get any output.
I have noticed that I only get an output if the value for pages from
var pages = parseInt(items)/10;
is greater than 1 , How do I fix this problem?

I think your problem lies elsewhere. Consider the following Math.ceil operations:
> Math.ceil(220/10)
22
> Math.ceil(134/10)
14
> Math.ceil(7/10)
1
Then look at the operation that you might have handled -- a string version of a number:
> Math.ceil(parseInt("7")/10);
1
> Math.ceil(parseInt(" 7")/10);
1
> Math.ceil(parseInt(" 7 ")/10);
1
It would appear that Math.ceil is providing 1 as expected, unless your value of 7 is malformed.

Related

How can i get all possibilities of binary in javascript

I have a uint8 array that in the first position have the value: 0xb00000000. If i console.log(uint8[0]) i receive 0 in the console.
I know if i do uint8[0] = uint8[0] ^ 0b00000001, the last bit will change to 1, then if i execute again console.log(uint8[0]) the console logs 1.
My question is: How can i get all 256 possible values using that same way to change the bits?
Is there a formule to do this?
Basically i need the console to show:
1
2
3
4
5
...

pagination logic in calculating total of pages

I'm using a react component that need me to pass in below prop
<Pager
totalPages={10}
currentPage={1}
/>
I can't figure out the calculation as in the api I have total_items, not totalPages. if I have 50 total_items, how can I produce 5 for the totalPages prop? says my limit is 10.
Divide total_items by limit, and round the value up.
Math.ceil(total_items/limit);
50 items / 10 per page = 5 pages
55 items / 10 per page = 6 pages
you need to check if there is a quotient from the divide operation because there is apparently few items need to be showed in another page
for example:
6 items
5 limit
if we just divide and ceil we will get 1 which is incorrect because there is one item need an extra page to be counted
totalPages_pre = (total_items/limit)
totalPages = (search.total % page_size) == 0 ? totalPages_pre : totalPages_pre + 1

Put a new tr row between highest and lowest values based on its number using jq/js

I have the following table in html built with json (there are columns that describe what the numbers are also):
1
2
7
10
11
15
Now from a button i want is when user enters a new row, I want the number to go in its place. E.g. if 4, it should be between 2 and 7. If 14, it should be between 11 and 15.
$('tbody#days > tr:nth-child(' + daynumber-1 + ')').after(new_row_data);
failed to work because the index couldn't be found. It works sometimes if for e.g. I have 3 and 5 and tried to insert 4.
I thought have looping the column top to bottom but i usually have 100-113 rows and I am wondering if there is a practical and efficient way of doing this.
Any tips?
The snippet you posted should work fine. However, this will throw an error if the all days are greater than the input.
For efficiency, you could maintain an array of already inserted days and populate it as more days are being inserted.
Before each insertion, look through the array of days and find a spot for that day and just insert it right away. Code for that will be
$('tbody#days > tr').eq( lowerIndex ).after( new_row_data );

Creating a trie (or tree) for a branch and bound multiple TSP

I'm struggling with increasing the speed of my pseudo branch & bound algo & thought a trie structure might be a good fit, but I'm stuck.
The problem (simplified & isolated best I can):
I've got 9 nodes & 3 vehicles. Each vehicle must visit 3 unique nodes. So, I created every possible trip (9 choose 3 = 84) & stuck it in an array. Now, I want to find every combination.
For example, trip 1 could be 111000000. trip 2 would be 000110001 and trip 3 would be 000001110. (84^3 = 592704 combinations).
To find out if they match, I just do a bitwise & and accept the trip combination if the value is 0.
I can't use nested loops since the # of vehicles may change, so I keep track of combinations with a counter that ticks up depth-wise like an odometer (e.g. 0,0,82, 0,0,83, 0,1,0).
I reduce combinations by keeping the following digit greater than the one that just increased (e.g. 11,83,83 goes to 12,13,14 because anything less than a 12 in the 2nd column would be a repeat like 12,1,1 is a duplicate of 1,1,12).
I also perform the bitwise AND check at each change (e.g. if (val[12] & val[13]) > 0 don't bother checking the 71 possibilities in the 3rd column, because the 12 and 13 invalidate the route. This reduces the combinations to 24720.
Since I'm doing depth-first, it's really space efficient (no queue to save), but computationally expensive. What I'd like to do is use a width-first approach to create subsets and minimize the search space. For example, assume the counter was at 11,19,20. Currently, it would check 20 - 83 in the 3rd column before incrementing the 19 to a 20. I would like to compute the AND for 19 - 83 in the 2nd column before moving on. In doing so, I would know all the values that don't work with the 11 in the first column and could use that subarray for the 3rd column (e.g. if (val[11] & val[45]) > 0, don't bother checking 11 & 19 & 45, rather use an array that excludes 45 from the 3rd column.
My idea is to create a trie, where each key is the result of the AND operation with an end key that would be the subarray.
For example:
doc = {
1: {
5: {
end: [8,9,10]
},
7: {
end: [11,14,42]
},
end: [81, 13, 42]
}
}
My problem is I can't for the life of me figure out how to iterate over my data to grow the trie. Or maybe there's a better approach? Any advice or code snippets would be great, and thanks for reading through the wall of text.

I need to divide an integer by 12 and if the result is a float, add 1 to it, in javascript

I just cannot figure this out, nor find any kind of similar question that makes any sense to me. My problem: I am extracting records from a database and displaying them in multiples of 12 per panel on my web page. I therefore need to know how many panels to display all records, using JavaScript (or possibly JQuery). Example:
records = 27;
panels = records / 12; //(which is 2.25)
Obviously I will need 3 panels to display all 27 records, but how can I get that from the result of 2.25? I've tried also using % instead of / but somehow I'm just not getting it.
records = 27; panels = Math.ceil(records / 12); // 3
Round up.
if result is not fully divisible by 12, then use Math.ceil (2.25) which equals 3

Categories