This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // User constructor | |
| function User(name, email) { | |
| this.name = name; | |
| this.email = email; | |
| this.isAdmin = false; | |
| } | |
| // Note: this "{}" represent the Constructor itself |
NewerOlder