Skip to content

Instantly share code, notes, and snippets.

@gregz67
Created November 12, 2015 18:00
Show Gist options
  • Save gregz67/137f388424897a626077 to your computer and use it in GitHub Desktop.
Save gregz67/137f388424897a626077 to your computer and use it in GitHub Desktop.
HackerRank quiz
/**
We need to deliver a package of Skittles bags. You will be given an inventory of small bags (1 kilo each) and big bags
(5 kilos each) along with the goal amount of kilos we need to ship the customer. Return the amount of small bags the package
will contain assuming we always use big bags first. Return -1 if it cannot be done.
Input
small (type: int) - The number of small bags we have to work with
big (type: int) - The number of big bags we have to work with
goal (type: int) - The goal weight of the package that we need to ship out
Output
In of how many small bags to use, if there isn't enough return -1.
*/
function getBags(small, big, goal) {
while (goal >= 5 && big > 0) {
goal = goal - 5;
// console.log(goal);
big--;
}
if (goal > small) {
return -1;
}
return goal;
}
console.log(getBags(10, 5, 12));
console.log(getBags(10, 5, 15));
console.log(getBags(1, 5, 12));
console.log(getBags(100, 5, 120));
console.log(getBags(100, 5, 0));
@naderahmed
Copy link

hi, your implimentation does not seem right, let me share with you my answer:

do{

if (number_of_big_bags_inventory != number_of_big_bags_inventory_tossed_in_crate){
number_of_big_bags_inventory_tossed_in_crate++
package_weight = package_weight + 5;
}
else if (number_of_small_bags_inventory != number_of_small_bags_inventory_tossed_in_crate){
number_of_small_bags_inventory_tossed_in_crate++
package_weight = package_weight + 1;

}

}while(package_weight <= KILOS_REQUESTED )

return number_of_small_bags_inventory_tossed_in_crate;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment