diff --git a/package-lock.json b/package-lock.json index a4cf1d6..b029eca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "diy-kata", - "version": "1.0.0", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1979,7 +1979,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2000,12 +2001,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2020,17 +2023,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2147,7 +2153,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2159,6 +2166,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2173,6 +2181,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2180,12 +2189,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2204,6 +2215,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2284,7 +2296,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2296,6 +2309,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2381,7 +2395,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2417,6 +2432,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2436,6 +2452,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2479,12 +2496,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/src/kata1.fizzBuzz.js b/src/kata1.fizzBuzz.js index 4b8c7f3..805b5d9 100644 --- a/src/kata1.fizzBuzz.js +++ b/src/kata1.fizzBuzz.js @@ -1,3 +1,18 @@ -const fizzBuzz = number => {}; +const fizzBuzz = number => { + return number % 3 === 0 && number % 5 === 0 ? "FizzBuzz" + : number % 3 === 0 ? "Fizz" + : number % 5 === 0 ? "Buzz" + : number; +}; + + module.exports = fizzBuzz; + + +// how to decide whic solution is best: +// Who is going to use or read the code? +// Do we want to prioritize readability? +// For example by putting the checking into separate clearly named functions +// that make it easy for any reader to know what's going on? +// or de want to avoid being verbose? \ No newline at end of file diff --git a/src/kata2.booleanToWord.js b/src/kata2.booleanToWord.js index 8602242..7aa3dcf 100644 --- a/src/kata2.booleanToWord.js +++ b/src/kata2.booleanToWord.js @@ -1,3 +1,5 @@ -const booleanToWord = boolean => {}; +const booleanToWord = boolean => { + return boolean ? "Yes" : "No"; +}; module.exports = booleanToWord; diff --git a/src/kata3.numberToReversedDigits.js b/src/kata3.numberToReversedDigits.js index cda9453..a7cc982 100644 --- a/src/kata3.numberToReversedDigits.js +++ b/src/kata3.numberToReversedDigits.js @@ -1,3 +1,15 @@ -const numberToReversedDigits = number => {}; +const numberToReversedDigits = number => { + const stringified = String(number); + const arrayed = stringified.split(""); + const numberfied = arrayed.map(char => Number(char)); + return numberfied.reverse(); +}; + + +// less verbose, more chained + +const numberToReversedDigitsChain = number => { + return String(number).split("").map(char => Number(char)).reverse(); +}; module.exports = numberToReversedDigits; diff --git a/src/kata4.humanCatDogYears.js b/src/kata4.humanCatDogYears.js index 6219e5e..7a66f03 100644 --- a/src/kata4.humanCatDogYears.js +++ b/src/kata4.humanCatDogYears.js @@ -1,3 +1,22 @@ -const humanCatDogYears = number => {}; +const calculateAnimalYears = (animal, number) => { + let factor = animal === "dog" ? 5 : animal === "cat" ? 4 : console.error("animal not recognised"); + + if (number === 1) { + animalYears = 15; + } else if (number === 2) { + animalYears = 24; + } else { + animalYears = ((number - 2) * factor) + 24; + } + return animalYears; +} + + +const humanCatDogYears = number => { + let catYears = calculateAnimalYears("cat", number) + let dogYears = calculateAnimalYears("dog", number) + return [number, catYears, dogYears] +}; module.exports = humanCatDogYears; + diff --git a/src/kata5.reachDestination.js b/src/kata5.reachDestination.js index a8e66a4..4facf83 100644 --- a/src/kata5.reachDestination.js +++ b/src/kata5.reachDestination.js @@ -1,3 +1,12 @@ -const reachDestination = (distance, speed) => {}; +const reachDestination = (distance, speed) => { + const time = (Math.ceil((distance / speed) * 2) / 2); + return `I should be there in ${time} hours.` +}; module.exports = reachDestination; + + + + + + diff --git a/src/kata6.joinNames.js b/src/kata6.joinNames.js index 760e0e4..5438d05 100644 --- a/src/kata6.joinNames.js +++ b/src/kata6.joinNames.js @@ -1,3 +1,17 @@ -const joinNames = namesObj => {}; +const joinNames = namesObjArray => { + const names = namesObjArray.map((person, index) => { + + if (index === namesObjArray.length - 1){ + return `& ${person.name}` + } else if (index === namesObjArray.length - 2){ + return `${person.name}` + } else { + return `${person.name},` + } + }) + + return names.join(' '); +}; module.exports = joinNames; + diff --git a/src/kata7.getEmployerRole.js b/src/kata7.getEmployerRole.js index 4f97ba7..68cddad 100644 --- a/src/kata7.getEmployerRole.js +++ b/src/kata7.getEmployerRole.js @@ -1,3 +1,18 @@ -const getEmployerRole = (employeeName, employees) => {}; +const getEmployerRole = (employeeName, employees) => { + return employees.find(emp => emp.name === employeeName).role; +}; + module.exports = getEmployerRole; + + + + + +//Function with 2 parameters including a employeeName(string) and an employees(array) + +///Write a function that takes in an array of objects and a string- which is a name + +////And we need to return from the function the employee role + +// e.g getEmployerRole('Satti', employees) should return 'Developer' \ No newline at end of file diff --git a/test/kata1.fizzBuzz.test.js b/test/kata1.fizzBuzz.test.js index 783d843..81029dc 100644 --- a/test/kata1.fizzBuzz.test.js +++ b/test/kata1.fizzBuzz.test.js @@ -1,11 +1,27 @@ const { fizzBuzz } = require("../src"); describe("fizzBuzz", () => { - test("returns Fizz when passed a multiple of 3", () => {}); + it("returns Fizz when passed a multiple of 3", () => { + expect(fizzBuzz(9)).toEqual("Fizz"); + expect(fizzBuzz(33)).toEqual("Fizz"); + expect(fizzBuzz(57)).toEqual("Fizz"); + }); - test("returns Buzz when passed a multiple of 5", () => {}); + it("returns Buzz when passed a multiple of 5", () => { + expect(fizzBuzz(10)).toEqual("Buzz"); + expect(fizzBuzz(35)).toEqual("Buzz"); + expect(fizzBuzz(100)).toEqual("Buzz"); + }); - test("returns FizzBuzz when passed a multiple 3 and 5", () => {}); + it("returns FizzBuzz when passed a multiple 3 and 5", () => { + expect(fizzBuzz(15)).toEqual("FizzBuzz"); + expect(fizzBuzz(45)).toEqual("FizzBuzz"); + expect(fizzBuzz(60)).toEqual("FizzBuzz"); + }); - test("returns the number when it isn't a multiple of 3 or 5", () => {}); + it("returns the number when it isn't a multiple of 3 or 5", () => { + expect(fizzBuzz(4)).toEqual(4); + expect(fizzBuzz(11)).toEqual(11); + expect(fizzBuzz(131)).toEqual(131); + }); }); diff --git a/test/kata2.booleanToWord.test.js b/test/kata2.booleanToWord.test.js index 867bbf3..68b075c 100644 --- a/test/kata2.booleanToWord.test.js +++ b/test/kata2.booleanToWord.test.js @@ -1,5 +1,12 @@ const { booleanToWord } = require("../src"); describe("booleanToWord", () => { - // how do we create specs again??? + + it((`should return "Yes" when passed true`), () => { + expect(booleanToWord(true)).toEqual("Yes"); + }); + + it((`should return "No" when passed false`), () => { + expect(booleanToWord(false)).toEqual("No"); + }); }); diff --git a/test/kata3.numberToReversedDigits.test.js b/test/kata3.numberToReversedDigits.test.js index 3b453ca..254bc85 100644 --- a/test/kata3.numberToReversedDigits.test.js +++ b/test/kata3.numberToReversedDigits.test.js @@ -1,5 +1,9 @@ const { numberToReversedDigits } = require("../src"); describe("numberToReversedDigits", () => { - test("returns a reversed array of the number's digits", () => {}); + const number = 12345; + + it("returns a reversed array of the number's digits", () => { + expect(numberToReversedDigits(number)).toEqual([5, 4, 3, 2, 1]); + }); }); diff --git a/test/kata4.humanCatDogYears.test.js b/test/kata4.humanCatDogYears.test.js index 8dbd198..60135e2 100644 --- a/test/kata4.humanCatDogYears.test.js +++ b/test/kata4.humanCatDogYears.test.js @@ -1,3 +1,27 @@ const { humanCatDogYears } = require("../src"); -// Look Ma, no handlebars!!! +describe("humanCatDogYears", () => { + it("should return an array with length 3", () => { + expect(humanCatDogYears(10)).toHaveLength(3) + }); + + it("should have the passed argument at index 0", () => { + expect(humanCatDogYears(10)[0]).toBe(10) + }); + + it("should have the equivalent cat years at index 1", () => { + expect(humanCatDogYears(10)[1]).toBe(56) + }); + + it("should have the equivalent dog years at index 2", () => { + expect(humanCatDogYears(10)[2]).toBe(64) + }); + + it("should return an array of 3 numbers: human, cat and dog years respectively", () => { + expect(humanCatDogYears(3)).toEqual([3, 28, 29]) + expect(humanCatDogYears(6)).toEqual([6, 40, 44]) + }) + +}); + + diff --git a/test/kata5.reachDestination.test.js b/test/kata5.reachDestination.test.js index d2bb1c6..7231e19 100644 --- a/test/kata5.reachDestination.test.js +++ b/test/kata5.reachDestination.test.js @@ -1,5 +1,19 @@ const { reachDestination } = require("../src"); describe("reachDestination", () => { - test("returns string with estimated time of arrival", () => {}); + test("returns a string", () => { + const result = reachDestination(44,10) + expect(typeof result).toBe("string"); + }); + + test("returns string with estimated time of arrival", () => { + expect(reachDestination(44, 10)).toBe('I should be there in 4.5 hours.') + expect(reachDestination(48, 10)).toBe('I should be there in 5 hours.') + expect(reachDestination(500, 10)).toBe('I should be there in 50 hours.') + }); + }); + + + + diff --git a/test/kata6.joinNames.test.js b/test/kata6.joinNames.test.js index 9936984..25fb1c9 100644 --- a/test/kata6.joinNames.test.js +++ b/test/kata6.joinNames.test.js @@ -1,5 +1,20 @@ const { joinNames } = require("../src"); describe("joinNames", () => { - test("returns string of names, seperated by commas and an ampersand", () => {}); + const theSimpsonsKids = + [{ name: 'Bart' }, + { name: 'Lisa' }, + { name: 'Maggie'}]; + + const theSimpsonsFamily = + [{ name: 'Bart' }, + { name: 'Lisa' }, + { name: 'Maggie' }, + { name: 'Homer' }, + { name: 'Marge' }]; + + test("returns string of names, separated by commas and an ampersand &", () => { + expect(joinNames(theSimpsonsKids)).toBe('Bart, Lisa & Maggie') + expect(joinNames(theSimpsonsFamily)).toBe('Bart, Lisa, Maggie, Homer & Marge') + }); }); diff --git a/test/kata7.getEmployerRole.test.js b/test/kata7.getEmployerRole.test.js index f5e2db8..ac48fe5 100644 --- a/test/kata7.getEmployerRole.test.js +++ b/test/kata7.getEmployerRole.test.js @@ -1,5 +1,22 @@ -const { getEmployerRole } = require("../src"); +const { getEmployerRole } = require("../src"); + const employees = [{ + name: 'Satti', + role: 'Developer' +}, { + name: 'Jenny', + role: 'Sales Associate' +}, { + name: 'Javid', + role: 'Human Recommended Reading Assistant' +}] describe("getEmployerRole", () => { - test("returns the employee's role in the company", () => {}); + test("returns the employee's role in the company", () => { + expect(getEmployerRole('Javid', employees)).toBe('Human Recommended Reading Assistant'); + expect(getEmployerRole('Satti', employees)).toBe('Developer'); + expect(getEmployerRole('Jenny', employees)).toBe('Sales Associate'); + }); }); + + +