From 40c908847adefcecb3ea0e9d3fb8c2d0e4d7c7a4 Mon Sep 17 00:00:00 2001 From: anna-m-b Date: Tue, 27 Oct 2020 21:27:07 +0000 Subject: [PATCH 1/3] completed katas 1 -3. Halfway through 4 --- package-lock.json | 43 ++++++++++++++++------- src/kata1.fizzBuzz.js | 26 +++++++++++++- src/kata2.booleanToWord.js | 4 ++- src/kata3.numberToReversedDigits.js | 14 +++++++- src/kata4.humanCatDogYears.js | 5 ++- test/kata1.fizzBuzz.test.js | 24 ++++++++++--- test/kata2.booleanToWord.test.js | 11 +++++- test/kata3.numberToReversedDigits.test.js | 6 +++- test/kata4.humanCatDogYears.test.js | 38 ++++++++++++++++++++ 9 files changed, 149 insertions(+), 22 deletions(-) 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..badb95f 100644 --- a/src/kata1.fizzBuzz.js +++ b/src/kata1.fizzBuzz.js @@ -1,3 +1,27 @@ -const fizzBuzz = number => {}; +const fizzBuzz = number => { + return number % 3 === 0 && number % 5 === 0 ? "FizzBuzz" + : number % 3 === 0 ? "Fizz" + : number % 5 === 0 ? "Buzz" + : number; +}; + + +// check if number is a multiple of 3, 5 or both + +// returns Fizz when passed a multiple of 3 + +// returns Buzz when passed a multiple of 5 + +// returns FizzBuzz when passed a multiple 3 and 5 + +// returns the number when it isn't a multiple of 3 or 5 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..20c9ab1 100644 --- a/src/kata4.humanCatDogYears.js +++ b/src/kata4.humanCatDogYears.js @@ -1,3 +1,6 @@ -const humanCatDogYears = number => {}; +const humanCatDogYears = number => { + let years = []; + +}; module.exports = humanCatDogYears; 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..9788cd7 100644 --- a/test/kata2.booleanToWord.test.js +++ b/test/kata2.booleanToWord.test.js @@ -1,5 +1,14 @@ 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"); + }); + + // how do we create specs again??? }); 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..55be100 100644 --- a/test/kata4.humanCatDogYears.test.js +++ b/test/kata4.humanCatDogYears.test.js @@ -1,3 +1,41 @@ const { humanCatDogYears } = require("../src"); // Look Ma, no handlebars!!! +describe("humanCatDogYears", () => { + it("should return an array of 3 numbers", () => { + 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 calculate cat and dog years according to the given human year", () => { + expect(humanCatDogYears(3).toEqual([3, 28, 29])) + expect(humanCatDogYears(6).toEqual([6, 40, 44])) + }) +}); + + +// Formulas for calculating cat and dog years +// Cat Years +// 15 cat years for first human year + +// +9 cat years for second human year + +// +4 cat years for each human year after that + +// Dog Years +// 15 dog years for first year + +// +9 dog years for second year + +// +5 dog years for each year after that \ No newline at end of file From fa4e6f088e19e139b7a5f4b65a0341d225c89cdf Mon Sep 17 00:00:00 2001 From: anna-m-b Date: Thu, 29 Oct 2020 08:32:21 +0000 Subject: [PATCH 2/3] completed humanCatDogYears kata --- src/kata4.humanCatDogYears.js | 41 +++++++++++++++++++++++++++-- test/kata4.humanCatDogYears.test.js | 7 ++--- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/kata4.humanCatDogYears.js b/src/kata4.humanCatDogYears.js index 20c9ab1..fad5d21 100644 --- a/src/kata4.humanCatDogYears.js +++ b/src/kata4.humanCatDogYears.js @@ -1,6 +1,43 @@ +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 years = []; - + let catYears = calculateAnimalYears("cat", number) + let dogYears = calculateAnimalYears("dog", number) + + return [number, catYears, dogYears] }; module.exports = humanCatDogYears; + + + + +// 10 - 2 = 8 +// 8 * 4 + 24 + +// Formulas for calculating cat and dog years +// Cat Years +// 15 cat years for first human year + +// +9 cat years for second human year + +// +4 cat years for each human year after that + +// Dog Years +// 15 dog years for first year + +// +9 dog years for second year + +// +5 dog years for each year after that \ No newline at end of file diff --git a/test/kata4.humanCatDogYears.test.js b/test/kata4.humanCatDogYears.test.js index 55be100..8b01af1 100644 --- a/test/kata4.humanCatDogYears.test.js +++ b/test/kata4.humanCatDogYears.test.js @@ -18,10 +18,11 @@ describe("humanCatDogYears", () => { expect(humanCatDogYears(10)[2]).toBe(64) }); - it("should calculate cat and dog years according to the given human year", () => { - expect(humanCatDogYears(3).toEqual([3, 28, 29])) - expect(humanCatDogYears(6).toEqual([6, 40, 44])) + 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]) }) + }); From 90e430ee3fa1534315873aad6f0255abed24471a Mon Sep 17 00:00:00 2001 From: anna-m-b Date: Tue, 3 Nov 2020 16:42:47 +0000 Subject: [PATCH 3/3] adding 5,6 & 7 --- src/kata1.fizzBuzz.js | 9 --------- src/kata4.humanCatDogYears.js | 21 --------------------- src/kata5.reachDestination.js | 11 ++++++++++- src/kata6.joinNames.js | 16 +++++++++++++++- src/kata7.getEmployerRole.js | 17 ++++++++++++++++- test/kata2.booleanToWord.test.js | 2 -- test/kata4.humanCatDogYears.test.js | 17 +---------------- test/kata5.reachDestination.test.js | 16 +++++++++++++++- test/kata6.joinNames.test.js | 17 ++++++++++++++++- test/kata7.getEmployerRole.test.js | 21 +++++++++++++++++++-- 10 files changed, 92 insertions(+), 55 deletions(-) diff --git a/src/kata1.fizzBuzz.js b/src/kata1.fizzBuzz.js index badb95f..805b5d9 100644 --- a/src/kata1.fizzBuzz.js +++ b/src/kata1.fizzBuzz.js @@ -6,15 +6,6 @@ const fizzBuzz = number => { }; -// check if number is a multiple of 3, 5 or both - -// returns Fizz when passed a multiple of 3 - -// returns Buzz when passed a multiple of 5 - -// returns FizzBuzz when passed a multiple 3 and 5 - -// returns the number when it isn't a multiple of 3 or 5 module.exports = fizzBuzz; diff --git a/src/kata4.humanCatDogYears.js b/src/kata4.humanCatDogYears.js index fad5d21..7a66f03 100644 --- a/src/kata4.humanCatDogYears.js +++ b/src/kata4.humanCatDogYears.js @@ -15,29 +15,8 @@ const calculateAnimalYears = (animal, number) => { const humanCatDogYears = number => { let catYears = calculateAnimalYears("cat", number) let dogYears = calculateAnimalYears("dog", number) - return [number, catYears, dogYears] }; module.exports = humanCatDogYears; - - - -// 10 - 2 = 8 -// 8 * 4 + 24 - -// Formulas for calculating cat and dog years -// Cat Years -// 15 cat years for first human year - -// +9 cat years for second human year - -// +4 cat years for each human year after that - -// Dog Years -// 15 dog years for first year - -// +9 dog years for second year - -// +5 dog years for each year after that \ No newline at end of file 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/kata2.booleanToWord.test.js b/test/kata2.booleanToWord.test.js index 9788cd7..68b075c 100644 --- a/test/kata2.booleanToWord.test.js +++ b/test/kata2.booleanToWord.test.js @@ -9,6 +9,4 @@ describe("booleanToWord", () => { it((`should return "No" when passed false`), () => { expect(booleanToWord(false)).toEqual("No"); }); - - // how do we create specs again??? }); diff --git a/test/kata4.humanCatDogYears.test.js b/test/kata4.humanCatDogYears.test.js index 8b01af1..60135e2 100644 --- a/test/kata4.humanCatDogYears.test.js +++ b/test/kata4.humanCatDogYears.test.js @@ -1,8 +1,7 @@ const { humanCatDogYears } = require("../src"); -// Look Ma, no handlebars!!! describe("humanCatDogYears", () => { - it("should return an array of 3 numbers", () => { + it("should return an array with length 3", () => { expect(humanCatDogYears(10)).toHaveLength(3) }); @@ -26,17 +25,3 @@ describe("humanCatDogYears", () => { }); -// Formulas for calculating cat and dog years -// Cat Years -// 15 cat years for first human year - -// +9 cat years for second human year - -// +4 cat years for each human year after that - -// Dog Years -// 15 dog years for first year - -// +9 dog years for second year - -// +5 dog years for each year after that \ No newline at end of file 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'); + }); }); + + +