Skip to content

Instantly share code, notes, and snippets.

@kowsheek
Last active January 25, 2021 07:58
Show Gist options
  • Save kowsheek/6a854cc5495d4a00c09837f40e6d3d7e to your computer and use it in GitHub Desktop.
Save kowsheek/6a854cc5495d4a00c09837f40e6d3d7e to your computer and use it in GitHub Desktop.
W1D5
function isValidArray(array) {
return array.length > 0;
}
function add(numbers) {
if (isValidArray(numbers)) {
var result = 0;
for (var i = 0; i < numbers.length; i++) {
result = result + numbers[i];
}
return result;
}
else {
throw new Error('Input is invalid');
}
}
function multiply(numbers) {
if (isValidArray(numbers)) {
var result = 1;
for (var i = 0; i < numbers.length; i++) {
result = result * numbers[i];
}
return result;
}
else {
throw new Error('Input is invalid');
}
}
module.exports = {
add: add,
multiply: multiply
};
{
"name": "w1d5",
"version": "1.0.0",
"description": "",
"main": "basicMath.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"left-pad": "^1.2.0"
},
"devDependencies": {
"assert": "^1.4.1"
}
}
var leftPad = require('left-pad');
var basicMath = require('./basicMath');
var testArray = [1, 2, 8];
console.log('add: ', basicMath.add(testArray));
console.log('multiply: ', basicMath.multiply(testArray));
console.log('---With leftPad---');
console.log(leftPad('add: ', 10, ' '), basicMath.add(testArray));
console.log(leftPad('multiply: ', 10, ' '), basicMath.multiply(testArray));
var basicMath = require('./basicMath');
var assert = require('assert');
describe('add()', function () {
it('should throw error if no numbers are given', function () {
assert(basicMath.add, Error, 'Input is invalid');
});
it('should return the result of adding the input numbers', function () {
var result = basicMath.add([1, 2, 3]);
assert.equal(result, 6);
});
it('should return the result of adding the input numbers', function () {
var result = basicMath.add([3, 5]);
assert.equal(result, 8);
});
});
describe('multiply()', function () {
it('should throw error if no numbers are given', function () {
assert(basicMath.multiply, Error, 'Input is invalid');
});
it('should return the result of multiplying the input numbers', function () {
var result = basicMath.multiply([1, 2, 3]);
assert.equal(result, 6);
});
it('should return the result of multiplying the input numbers', function () {
var result = basicMath.multiply([3, 5]);
assert.equal(result, 15);
});
});

Background

  1. Objects: collection of key-value pairs, where keys are unique
  2. Functions: they're contracts, they take in inputs, they usually output some value
  3. Modules: collection of objects and functions that is "exported" and can be "imported"
  4. Packages: description of one or more modules that are available to a developer

Modules

  1. Modules are not special
  2. module.exports
  3. Importing modules

npm

  1. Collection of packages
  2. package.json
  3. npm init
  4. npm install & npm install -g & npm install --production
  5. npm install --save & npm install --save-dev

Testing

  1. Installing Mocha
  2. describe
  3. it
  4. assert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment