Skip to content

Instantly share code, notes, and snippets.

@donburks
Created October 6, 2017 18:51
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 donburks/6519b1c79fac1d6382a1c126d45f31e3 to your computer and use it in GitHub Desktop.
Save donburks/6519b1c79fac1d6382a1c126d45f31e3 to your computer and use it in GitHub Desktop.
add(2)(3);
// 5
function add(x) {
return function(y) {
return x + y;
};
}
console.log(add(2)(3)); //5
var waitForLater = add(2);
//Wait for one bajillion years
console.log(waitForLater(3));
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var mul = require('./multiply');
list.forEach(mul.multiply);
var leftpad = require('left-pad');
console.log(leftpad("kitten", 10, "-"));
var numbers = process.argv.slice(2);
var math = require('./mathLib');
/*console.log("RANGE: ", math.range(Number(numbers[0]), Number(numbers[1])));
console.log("FACTORIAL: ", math.factorial(Number(numbers[0]), Number(numbers[1])));
*/
var x = Number(numbers[0]);
var y = Number(numbers[1]);
console.log("RANGE: ", math.doMath(x, y, "range"));
console.log("FACTORIAL: ", math.doMath(x, y, "factorial"));
function doStuff(x, y) {
console.log("Starting with " + x + " and " + y);
}
function range(x, y) {
doStuff(x, y);
if (x > y) {
return false;
}
var results = { range: [] };
for (var i = x; i <= y; i++) {
results.range.push(i);
}
return results;
}
function factorial(x, y) {
doStuff(x, y);
var factorial = x;
for (var j = x; j <= y; j++) {
factorial *= j;
}
return factorial;
}
function doMath(x, y, operation) {
if (operation == "range") {
return range(x, y);
} else {
return factorial(x, y);
}
}
module.exports = {
doMath: doMath
};
var num = 2;
function multiply(x) {
console.log(x * num);
}
module.exports = {
multiply: multiply
}
{
"name": "formatter",
"version": "1.0.0",
"description": "Demonstrating npm and package.json for the Oct 02, 2017 cohort",
"main": "formatter.js",
"directories": {
"test": "test"
},
"dependencies": {
"left-pad": "^1.1.3"
},
"devDependencies": {},
"scripts": {
"test": "mocha"
},
"keywords": [
"left-pad",
"npm",
"demo"
],
"author": "Don Burks <don@donburks.com> (https://donburks.com)",
"license": "ISC"
}
var assert = require('assert');
var math = require('../mathLib');
describe("Range function", function() {
it("should return an array", function() {
var result = math.doMath(1, 5, "range");
assert.equal(result.range instanceof Array, true);
});
it ("should return an array from 1 to 5 when given parameters of 1 and 5", function() {
var result = math.doMath(1, 5, "range");
assert.deepEqual(result.range, [1, 2, 3, 4, 5]);
});
it("should fail if x is greater than y", function() {
var result = math.doMath(5, 1, "range");
assert.equal(result, false);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment