I am trying to understand a knapsack algorithm given to me in class, specifically the following pseudocode.

This was my attempt to code it:
//Init array
var solution = [];
var items = problem.items;
var sackSize = problem.knapsack;
solution[0] = 0;
for(var k = 1; k <= sackSize; k++)
{
var loot = 0;
for(var i = 1; i <= items.length; i++)
{
if(k >= items[i].weight)
{
loot = Math.max(loot, items[i].value)
}
}
solution[k] = loot;
}
This doesn't make sense to me because if(k >= items[i].weight) always gives an "index out of bounds" error on the last iteration of the loop. The items array starts at index 0 but i starts at 1. Why are we starting at index 1? Am I misinterpreting the variables?
I am given:
The object problem includes the maximum weight of the knapsack (problem.knapsack) and an array of available items (problem.items). Each item is an object with a weight and value attribute (problem.items[i].weight and problem.items[i].value). Both of these functions should return an array of selected items. The items in the returned array should also have weight and value attributes.
1tonrather than0ton-1. Just because the pseudo code iterates from1tondoes not mean your code also has to iterate from1tonespecially when you know that array index starts at0. - wookie919