Skip to content

Instantly share code, notes, and snippets.

@shixish
Created May 11, 2014 05:29
Show Gist options
  • Save shixish/bc421315b8034f58e0f3 to your computer and use it in GitHub Desktop.
Save shixish/bc421315b8034f58e0f3 to your computer and use it in GitHub Desktop.
var assert = require('assert');
/********************************
* We want make a package of goal kilos of chocolate. We have
* inventory of small bars (1 kilos each) and big bars (5 kilos each).
* Return the number of small bars to use, assuming we always
* use big bars before small bars. 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 chocolate.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
********************************/
var bigBars = 5;
var smallBars = 1;
function makeChocolate(small, big, goal) {
var desired_big = Math.floor(goal/bigBars);
//console.log('big', desired_big);
var desired_small = goal - (bigBars*desired_big); //parseInt(goal%bigBars);
//console.log('small', desired_small);
if (desired_small <= small && desired_big <= big){
return desired_small;
} else {
return -1;
}
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
assert.equal(
makeChocolate(4, 1, 9),
4
);
assert.equal(
makeChocolate(4, 1, 10),
-1
);
assert.equal(
makeChocolate(4, 1, 7),
2
);
assert.equal(
makeChocolate(6, 2, 7),
2
);
assert.equal(
makeChocolate(4, 1, 5),
0
);
assert.equal(
makeChocolate(4, 1, 4),
4
);
assert.equal(
makeChocolate(5, 4, 9),
4
);
assert.equal(
makeChocolate(9, 3, 18),
3
);
console.log('Success!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment