var assert = { test: 1, equal: function(exp, target) { console.log("[test](%d) expected "+target+" got " + exp + " (%s)", this.test++, (exp == target ? 'SUCCESS' : 'FAIL')); } }; // SKITTLES // Given a target goal in kilograms, and a number of // small bags of skittles (1kg) and large bags of skittles (5kg) // return the number of small bags used to make up the total, // assuming we'll use as many big bags as possible. // If not possible, return -1. // function createPackage(small, big, goal) { var lg = Math.floor(goal / 5), sm = Math.floor(goal - lg); goal = goal - ((big <= lg) ? (big*5) : (lg*5)); return (small >= goal) ? goal : -1; } 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!');