Skip to content

Instantly share code, notes, and snippets.

@clayzermk1
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clayzermk1/11105447 to your computer and use it in GitHub Desktop.
Save clayzermk1/11105447 to your computer and use it in GitHub Desktop.
iFit
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
********************************/
function makeChocolate(small, big, goal) {
// Constant sizes
var BIGKG = 5, SMALLKG = 1;
// Accumulators
var bigsNeeded = 0, smallsNeeded = 0;
// Check to see that we have enough chocolate to meet the goal
if ((small * SMALLKG) + (big * BIGKG) < goal) {
return -1; // we don't have enough chocolate!
}
while (goal > 0) {
if (goal / BIGKG >= 1 && big > 0) {
goal -= BIGKG;
bigsNeeded ++;
big --;
}
else if (goal / SMALLKG >= 1 && small > 0) {
goal -= SMALLKG;
smallsNeeded ++;
small --;
}
}
// sanity check
if (goal !== 0) {
throw new Error('something went wrong');
}
return smallsNeeded;
}
/********************************
* 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!');
var assert = require('assert');
/********************************
* Return true if the array contains, somewhere, three increasing
* adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.
*
* 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 tripleUp.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 tripleUp(array) {
for (var i = 0; i < array.length - 2; i ++) {
// Look ahead at the next two numbers to check for a sequence
if ((array[i] === array[i + 1] - 1) && (array[i] === array[i + 2] -2)) {
return true;
}
}
return false;
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
assert.equal(
tripleUp([1, 4, 5, 6, 2]),
true
);
assert.equal(
tripleUp([1, 2, 3]),
true
);
assert.equal(
tripleUp([1, 2, 4, 5, 7, 6, 5, 6, 7, 6]),
true
);
assert.equal(
tripleUp([1, 2, 4, 5, 7, 6, 5, 7, 7, 6]),
false
);
assert.equal(
tripleUp([1,2]),
false
);
assert.equal(
tripleUp([10, 9, 8, -100, -99, -98, 100]),
true
);
assert.equal(
tripleUp([10, 9, 8, -100, -99, 99, 100]),
false
);
console.log('Success!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment