Skip to content

Instantly share code, notes, and snippets.

@McQuinTrix
Created June 17, 2015 17:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McQuinTrix/3220d8277aa80e87ec48 to your computer and use it in GitHub Desktop.
Save McQuinTrix/3220d8277aa80e87ec48 to your computer and use it in GitHub Desktop.
var assert = require('assert');
/********************************
* We want make a package of goal kilos of skittles. We have
* inventory of small bags (1 kilos each) and big bags (5 kilos each).
* Return the number of small bags to use, assuming we always
* use big bags before small bags. Return -1 if it can't be done.
*
* See the asserts below for examples of input
* and expected output.
*
* If you have node installed, all you need to do to test
* your code is run: `node skittles.js`. If you see errors,
* it is because the tests below did not pass. Once the
* tests do pass, you will see a log of `Success!`
*
* YOUR CODE BELOW HERE
********************************/
function createPackage(small, big, goal) {
var count=0;
while(goal/5 >= 1 && big > 0){
goal -= 5;
big -= 1;
}
if(goal == 0){
return 0;
}else if(goal<1){
return -1;
}
while(goal >0 && small > 0){
count++;
goal -= 1;
small -= 1;
}
if(goal == 0){
return count;
}else{
return -1;
}
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
assert.equal(
createPackage(4, 1, 9),
4
);
assert.equal(
createPackage(4, 1, 10),
-1
);
assert.equal(
createPackage(4, 1, 7),
2
);
assert.equal(
createPackage(6, 2, 7),
2
);
assert.equal(
createPackage(4, 1, 5),
0
);
assert.equal(
createPackage(4, 1, 4),
4
);
assert.equal(
createPackage(5, 4, 9),
4
);
assert.equal(
createPackage(9, 3, 18),
3
);
console.log('Success!');
@martinvanha
Copy link

martinvanha commented Jan 18, 2017

Couldn't it be without loops? Something like this would do the trick:

function createPackage(small, big, goal) {
    // Calculate number of bigs
    var b = Math.floor(goal / 5);

    // Check if there is enough
    if (b > big) {
        // It is not - use all there is
        b = big;
    }

    // Calculate number of smalls
    var s = goal - 5 * b;

    // Check if there is enough
    if (s > small) {
        // It is not - cannot do it
       s = -1
    }

    return s;
}

console.log('createPackage(2, 4, 22) ' + (createPackage(2, 4, 22) === 2 ? 'succeeded' : 'failed'));
console.log('createPackage(7, 3, 22) ' + (createPackage(7, 3, 22) === 7 ? 'succeeded' : 'failed'));
console.log('createPackage(6, 3, 22) ' + (createPackage(6, 3, 22) === -1 ? 'succeeded' : 'failed'));
console.log('createPackage(0, 4, 20) ' + (createPackage(0, 4, 20) === 0 ? 'succeeded' : 'failed'));
console.log('createPackage(10, 0, 20) ' + (createPackage(10, 0, 20) === -1 ? 'succeeded' : 'failed'));
console.log('createPackage(20, 0, 20) ' + (createPackage(20, 0, 20) === 20 ? 'succeeded' : 'failed'));

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