Created
November 12, 2015 18:00
-
-
Save gregz67/137f388424897a626077 to your computer and use it in GitHub Desktop.
HackerRank quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;