Forum Moderators: open
and I have an input: weight, eg 400.
My problem is how to create a summary from the values of the table that won't exceed the weight in input.
In this case it would be sum of 1+2, because 1+2+3 will exceed given weight. the answer I need is: [300, 300000, 198] and a number 2 (on which row the counting ended).
The count has to be done by fifo (first in first out = starting from the top).
please help, or at least say if that cannot be done in javascript, then I'll turn to server side script php.
Michal Cibor
Array.prototype.vectorAdd = function(V){
for(var i=0;i<this.length;) this[i]+=V[i++]
}var table =
[
[100,100000,100],
[200,200000, 98],
[300,400000, 50]
];[blue]/*
Don't know what kind of real table you are using.
I have thrown in this function to abstract the
process of getting an array of values from the
table.If the 'table' is an HTML table, all that's
needed is to change this function's implementation.
! swap the ¦¦ for unbroken pipes!
*/[/blue]
function getRowVector(table,iRow)
{
return table[iRow]¦¦null;
}function getSummary(maxWeight)
{
var Vsum = [0,0,0];
var totalWeight=0,r=-1, row;
[blue]/* could just do: row = table[++r] */[/blue]
while( row = getRowVector(table,++r))
{
if( (totalWeight += row[0]) > maxWeight) break;
Vsum.vectorAdd(row);
}
[blue]/* Add r to front of array.
It's already 1 ahead of row index, so it suits row no. */[/blue]
Vsum.unshift(r);
return Vsum;
}alert( '['+getSummary(400)+']' );
Thanks a lot.
BTW I knew theoretically how to do this, but don't have any experience with arrays in javascript, therefore the script provided me with great insight into making and operating an array.