Find an intermediate value for MOD factor - javascript

I am developing an app. At 5 pm, the array resets and count starts from 0 and by the end of day there are thousands of values inside the array. There is a very simple code inside my logic. My data is inside the array data.
data.forEach(function(entry) {
if (entry.time_stamp >= tsYesterday) {
if (count % 100 == 0) {
element = { x: new Date(entry.time_stamp), y: entry.occupied_count };
history_slots.push(element);
}
count++;
}
But the issue is that at 5 pm when there is just 1 element inside the array, it doesn't gets displayed because (0-99)%100 so I want to replace 100 with something. Can I change the value of MOD Factor on the basis is array length? Please guide me if you understand my question.
Thanks in advance.

Related

vue.js pagination mis calculation

In my app, I have some pagination code which calculates the pagination based on data from a REST API. When I add the page of pages, it is calculating from page 0 not from 1, so it says 0 of 9 and when it gets to the end it says 8 of 9, when it should say 1 of 9 and 9 of 9 at the end. So far my code is:
HTML
<p>Page {{page}} of {{pageCount}}</p>
JS
data: function() {
return {
page: 0
};
},
computed: {
pageCount() {
let l = this.result.length,
s = this.size;
return Math.floor(l / s);
},
paginated() {
const start = this.page * this.size,
end = start + this.size;
return this.result.slice(start, end);
}
},
Any ideas? Maybe I am calculating the math.floor method wrong?
Your page variable is 0 indexed instead of 1 index. You can keep it this way so that your pagination continues to work as intended, but where you are outputting it to the user you can simply add 1 so it makes sense to a user.
<p>Page {{page + 1}} of {{pageCount}}</p>
From what I understood, your pageCount() method is correct because you indeed have 9 pages so math.floor is not the problem, your page variable is the one that is incorrect but I cannot see where you are getting that variable from but a simple yet coarse solution would be just to add 1 to the variable.

javascript making change algorithm

I'm solving this problem "Making change" in javascript:
Question:
Given an amount of money, an array of coin denominations, compute the
number of ways to make the amount of money with coins of the available
denominations.
Example:
For amount=4 (4¢) and denominations=[1,2,3] (1¢,
2¢ and 3¢), your program would output 4—the number of ways to make
4¢ with those denominations:
1¢, 1¢, 1¢, 1¢
1¢, 1¢, 2¢
1¢, 3¢
2¢, 2¢
I found a solution:
var makeChange = function(total){
var count = 0;
var coins = [1, 2, 5, 10, 20, 50, 100, 200];
var changer = function(index, value){
var currentCoin = coins[index];
if( index === 0){
if( value % currentCoin === 0){
count++;
}
return;
}
while( value >= 0 ){
changer(index-1, value);
value -= currentCoin;
}
}
changer(coins.length-1, total);
return count;
};
makeChange(200);
Problem(s):
Can someone explain to me what is going on? I tried following the code but i get lost in between the recursion.
I understand that he is taking the final coin value and he is substracting from the given total. (But why?) I'm kinda lost.
When value >= 0 in the while loop, It keeps looping around increasing the index, i couldn't understand why.
Can someone make sense out of this algorithm?
Sorry, just started learning Dynamic Programming.
Thank you,
Let's track what happens with makeChange(4):
The function changer gets defined then called for the first time.
value = 4, index = 7, coins[7] = 200
Since the variable, index is not 0, we move on to the while loop.
A second call to changer is made with index 6
Meanwhile, the first call continues the 'while'
loop but since 200 has been subtracted from 'value',
'value' is now less than 0 so the 'while' loop terminates
and this first call does nothing more.
(Keep in mind that the variable 'value' is distinct
and private to each call, so the 'while' loop only
affects the 'value' in its own function call.)
Ok, now this pattern continues with all the function calls that have index pointing to a coin larger than value until index is 1.
value = 4, index = 1, coins[1] = 2
This time more happens in the while loop:
We get the function call, 'changer(0,4)',
AND a second function call, 'changer(0,2)',
after we subtract 2 from 'value', which was 4,
AND a third function call, 'changer(0,0)',
after we subtract 2 from 'value', which was 2.
These 3 calls respectively represent:
1 + 1 + 1 + 1
2 + 1 + 1
2 + 2
Each time the line 'value -= currentCoin' is executed,
it represents the start of another set of choices for
solutions that include that coin.
4 % coins[0] = 0, meaning 4 is divisible by 1 represents 1 + 1 + 1 + 1
4 - 2 folllowed by 2 % 1 represents 2 + 1 + 1
and 4 - 2 - 2 represents 2 + 2
Total count: 3

Take value of variable and run it through array

I am in the process of building a calculator to determine how long a childs colic is going to last.
I have the calculator working correctly just need it to take the value it gets and run it through the array and match the next closest value to the value shown and then reference it to its associated week.
So for instance if you select week 2 and then week 5 the calculation returns childs colic will end in .5614399999999999 weeks. You would then run the .5614 through the wessel_data array and find that .5614 falls in between week 6 and 7. I would then take the next closest week which is week 7 and show that instead of the .5614. So it should say childs colic will end in .64 weeks. Now that it has found the .64 I want it to output the associated weeks so that it would say childs colic will end in 7 weeks.
This is what I have written to find the next associated value but cant get it to work.
function closest (num, wessel_data) {
var curr = wessel_data[0];
var diff = Math.abs (num - curr);
for (var val = 0; val < wessel_data.length; val++) {
var newdiff = Math.abs (num - wessel_data[val]);
if (newdiff < diff) {
diff = newdiff;
curr = wessel_data[val + 1];
}
}
return curr;
}
I also have a fiddle so you can see what I am talking about.
Fiddle
I think you want the index of the wessel_data, not the data itself. That is just for comparing. So in this function set curr = val + 1. Not wessel_data[val + 1].
You aren't using this function at all in your fiddle so it is hard to tell what exactly you are doing.
My advice is to think of what values you want returned given inputs and work backward from there.

Bucketing numbers in an array

I have a list of numbers e.g 1-to 60, but not necessarily in increments of 1. E.g 1-40 in increments of 1, and 40-60 in increments of two.
I have another set of defined numbers (buckets) –e.g 2, 3, 5 , 10, 30, 50
I need to produce a two dimensional array with percentages of where each number from above (1 to 60) fits into which bucket.
to make this simpler: let’s say we have numbers 1 to 10, and buckets, 2, 3, 5, 10
I want my two dimensional array to look like this:
I can do this with a bunch of conditionals, but I think there’s a solution out there which I have not thought of and would be great if someone could shed some light!
I need to do this in JavaScript, but any language would help me to try and understand any solution more optimal than lots of if’s deciding where each number fits and then doing (6-5/10-5)=0.2 for each cell.
I’m trying to avoid hardcoding buckets, 2, 3, 5, 10 so that any set of buckets or numbers can do the job.
EDIT:
First of all, I'm sorry for the incomplete description - I was on my phone at the time and couldn't post on stackoverflow via a computer.
Both 1-10 and 2,3,5,10 represent years. Effectively, I'm trying to bucket each year from 1 to 10.
Year 1 goes 100% into Bucket 2 - I guess there isn't a specific
formula for this cell
Year 2 goes 100% into Bucket 2 - No specific formula either
Year 3 goes 100% into Bucket 3 - 3==3
Year 4 is split half between Bucket 3, and half between Bucket 5. The formula for this is: (Year 4 - Bucket 3)/(Bucket 5 - Bucket 3) = 0.5
Year 5 goes 100% into Bucket 5.
Year 6, goes 80% into Bucket 5, and 20% into Bucket 10. The formula for this is: 1-(6-5)/(10-5)=0.8 and for its neighbouring cell (6-5)/(10-5)
...and so on...
I hope this makes it clearer.
You could do something like this. It puts it in exactly the format you asked for in the post:
function bucketize(numberList, buckets) {
// buckets must contain values sorted from smallest to largest
var bucketized = [];
var i, j, lowBucket, difference, bucketSpan, ratio;
for (i=0; i<numberList.length; i++) {
bucketized[i]=new Array(buckets.length + 1);
bucketized[i][0]=numberList[i];
lowBucketIndex=null;
for (j=0; j<buckets.length; j++) {
if (lowBucketIndex === null && numberList[i] < buckets[j]) {
lowBucketIndex=j-1;
if (lowBucketIndex < 0) {
// this bucket gets it all
bucketized[i][j+1]=1;
} else {
//divide this value between buckets
difference = numberList[i] - buckets[lowBucketIndex];
bucketSpan = buckets[j] - buckets[lowBucketIndex];
ratio=difference/bucketSpan;
bucketized[i][lowBucketIndex+1] = 1-ratio;
bucketized[i][j+1] = ratio;
}
} else {
bucketized[i][j+1]=0;
}
}
if (lowBucketIndex === null) {
bucketized[i][buckets.length] = 1;
}
}
return bucketized;
}
var buckets = [2,3,5,10];
var numberList=[1,2,3,4,5,6,7,8,9,10];
var bucketized = bucketize(numberList, buckets);
var i;
for (i=0; i<bucketized.length; i++) {
console.log(bucketized[i].join(','));
}
Here's a fiddle.

Javascript - return array of values that totals next number up

I am trying to work out the best way to achieve the following:
A score might require a total of 25 'items' AS A MINIMUM
currently that person might have 15.8 'items'
I have a number of items required to reach that score (9.2)
So to get that score the minimum a person must have is 10 'items' in x weeks (to be 25.8 and over the 25 threshold).
Assuming they have 4 weeks to do it that gives 2.5 needed per week.
What I am struggling with is how to output an array of 'whole items' that will make 10.
e.g.
2
3
2
3
I want the items to be as evenly distributed as possible.
so
1
2
3
4
would be useless
I am just trying to work out the best way to do this.
I was thinking of:
finding nearest whole number ROUNDED UP (3 in this example)
Then outputting that number
Then on the next pass gathering the 'remainder' (-0.5) and then rounding the number.
But it doesn't quite work for all cases.
Can anyone help me get my head around it without writing hundreds of lines of code.
As a further example say 17 were needed in 5 weeks that would be
4
3
3
4
3
Thanks in advance.
You could do it some way like this:
function myFunction(total, weeks) {
var n = 0;
var temp = 0;
var arr = [];
while (n < total) {
temp = n;
n += total / weeks;
arr.push(Math.floor(n) - Math.floor(temp));
}
return arr;
}
myFunction(10, 4); //[2, 3, 2, 3]
myFunction(17, 5); //[3, 3, 4, 3, 4]
In this code, total / weeks will be the average number you'll want to add to n to get from 0 to total in exactly weeks iterations.
Now the difference between the original value of n (which is stored in temp) and the new value of n is pushed to the array. That will give you the rounded numbers you'll need to add up to the entered total.

Categories