|
| 1 | +let count = 1; |
| 2 | +let totalProfit = 0; |
| 3 | + |
| 4 | +function addItem(){ |
| 5 | + count = count + 1; |
| 6 | + let container = document.getElementById("itemInformations"); |
| 7 | + container.appendChild(document.createTextNode("Item " + count + " ")); |
| 8 | + // Create an <input> element, set its type and name attributes |
| 9 | + let input = document.createElement("input"); |
| 10 | + input.type = "number"; |
| 11 | + input.name = "item" + count; |
| 12 | + input.id = "item" + count; |
| 13 | + input.class = "item"; |
| 14 | + input.placeholder = "Enter Weight (KG)" |
| 15 | + |
| 16 | + container.appendChild(input); |
| 17 | + |
| 18 | + container.appendChild(document.createTextNode(" Profit " + count + " ")); |
| 19 | + // Create an <input> element, set its type and name attributes |
| 20 | + let input2 = document.createElement("input"); |
| 21 | + input2.type = "number"; |
| 22 | + input2.name = "profit" + count; |
| 23 | + input2.id = "profit" + count; |
| 24 | + input.class = "item"; |
| 25 | + input2.placeholder = "Enter Profit"; |
| 26 | + |
| 27 | + container.appendChild(input2); |
| 28 | + |
| 29 | + // Append a line break |
| 30 | + container.appendChild(document.createElement("br")); |
| 31 | +} |
| 32 | + |
| 33 | +function deleteItem(){ |
| 34 | + |
| 35 | + // Container <div> where dynamic content will be placed |
| 36 | + let container = document.getElementById("itemInformations"); |
| 37 | + // 5 remove child is called to remove child (br, input ,text) for 1 item |
| 38 | + if (container.hasChildNodes() && count!=1) { |
| 39 | + container.removeChild(container.lastChild); |
| 40 | + container.removeChild(container.lastChild); |
| 41 | + container.removeChild(container.lastChild); |
| 42 | + container.removeChild(container.lastChild); |
| 43 | + container.removeChild(container.lastChild); |
| 44 | + count = count - 1; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function resetBtn(){ |
| 49 | + let resultContainer = document.getElementById("result"); |
| 50 | + |
| 51 | + while(resultContainer.hasChildNodes){ |
| 52 | + resultContainer.removeChild(container.lastChild); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +let theItems = []; |
| 57 | + |
| 58 | +function 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 | + |
| 116 | + theItems.sort(function(a,b){ |
| 117 | + if (a.name < b.name) |
| 118 | + return -1; |
| 119 | + if (a.name > b.name) |
| 120 | + return 1; |
| 121 | + |
| 122 | + return 0; |
| 123 | + }); |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + let finalResult = ""; |
| 128 | + for(let i = 0; i < count; i++){ |
| 129 | + finalResult = finalResult.concat(theItems[i].amount.toString()) + " , "; |
| 130 | + } |
| 131 | + |
| 132 | + finalResult = finalResult.replace(/,\s*$/, ""); |
| 133 | + |
| 134 | + let resultContainer = document.getElementById("result"); |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + resultContainer.appendChild(document.createTextNode("( " + finalResult + ")")); |
| 139 | + |
| 140 | + resultContainer.appendChild(document.createElement("br")); |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +} |
0 commit comments