MCPcopy Create free account
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / calculate

Function calculate

assets/js/KnapsackProblem.js:58–145  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

56let theItems = [];
57
58function calculate(){
59
60 for(let i = 0; i< count; i++){
61 theItems[i] = { name: "thename", weight: 0, profit: 0, profitWeight: 0, amount: 0};
62 }
63 //let profitWeight = [count];
64 let knapsackWeight = document.getElementById("knapsackWeight").value;
65
66 container = document.getElementById("itemInformations");
67
68 if(count >= 1){
69 for(let x = 1, j = 0; j < count; j++, x++){
70
71 let itemName = "item" + x.toString();
72 let profitName = "profit" + x.toString();
73
74 let weight = document.getElementById(itemName).value;
75 let profit = document.getElementById(profitName).value;
76
77 theItems[j].profit = profit;
78 theItems[j].weight = weight;
79 theItems[j].name = itemName;
80 theItems[j].profitWeight = profit/weight;
81
82 }
83 let flag = count;
84
85 //sorting the array of items decending order of profitWeight
86 theItems.sort(function(a,b){
87 if (a.profitWeight > b.profitWeight)
88 return -1;
89 if (a.profitWeight < b.profitWeight)
90 return 1;
91
92 return 0;
93 });
94
95 for(let i = 0; i < count; i++){
96
97 if (knapsackWeight >= theItems[i].weight){
98 //
99 knapsackWeight = knapsackWeight - theItems[i].weight;
100 //setting the amount to 1 as it will be chosen
101 theItems[i].amount = 1;
102 totalProfit += theItems[i].profit;
103
104 }
105 else{
106 //set the amount to be taken as 0 as it will not be pick
107 theItems[i].amount = 0;
108 }
109 }
110
111
112
113
114 }
115

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected