Skip to content

Instantly share code, notes, and snippets.

View iKnowJavaScript's full-sized avatar
🎯
Focusing

Victor Omolayo iKnowJavaScript

🎯
Focusing
View GitHub Profile
// one liner
const sum = (arr) => arr.reduce((acc, item) => acc + item, 0);
const doubleNumber = (arr) => arr.map((item) =>item * 2)
//dont forget to export
module.exports = { sum, doubleNumber }
@iKnowJavaScript
iKnowJavaScript / logic.test.js
Last active May 2, 2019 01:49
Test file for logic.js
// reguire the real file to be tested
const { sum, doubleNumber} = require("./logic.js") //use correct path
describe("Sum function should return the sum of all the numbers in an array", function() {
it("Should return 4 for [1,2,1]", function() {
expect(sum([1,2,1])).toEqual(4)
})
it("Should return 10 for [1,2,3,4,5]", function() {
expect(sum([1,2,3,4,5])).toEqual(15)
})
@iKnowJavaScript
iKnowJavaScript / logic.js
Last active May 2, 2019 01:43
Getting started with jest testing
function sum(arr) {
let total = 0;
for(let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
const numbers = [1,2,3,4,5]
sum(numbers); //15
function Admin(name, email) {
User.call(this, name, email);
this.isAdmin = true;
}
Admin.prototype = Object.create(User.prototype);
Admin.prototype.constructor = Admin;
// we can now add methods and props to Admin Prototype
Admin.prototype.deleteAllComment = function() {
// User constructor
function User(name, email) {
this.name = name;
this.email = email;
this.isAdmin = false;
}
// We can now add methods to its prototype like so...
User.prototype.postComment = function(message) {
//add Post implementation here
@iKnowJavaScript
iKnowJavaScript / User.js
Last active April 29, 2019 08:44
Demo app
// User constructor
function User(name, email) {
this.name = name;
this.email = email;
this.isAdmin = false;
}
// Note: this "{}" represent the Constructor itself