From 1a170aaffa70e255b97605cfb7107814857e7267 Mon Sep 17 00:00:00 2001 From: smita81 Date: Fri, 19 Apr 2024 20:11:21 +0200 Subject: [PATCH] test check --- .../exercises/easy/count-down.test.js | 48 +++++++++++++++-- .../exercises/easy/count-up.test.js | 43 +++++++++++++-- .../exercises/easy/reverse-a-string.test.js | 20 +++++++ .../exercises/easy/reverse-and-case.test.js | 52 ++++++++++++++++-- .../exercises/easy/set-the-case.test.js | 51 ++++++++++++++++-- .../testing-paths-if-else-if-else.test.js | 29 +++++++--- .../easy/testing-paths-if-else.test.js | 30 +++++++---- ...ting-paths-sequential-conditionals.test.js | 21 +++++--- .../exercises/hard/numbery-numberify.test.js | 44 +++++++++++++-- .../exercises/hard/passing-objects.test.js | 38 ++++++++----- .../exercises/hard/sum-numbery.test.js | 37 +++++++++++-- .../exercises/medium/fizzbuzz-1.test.js | 53 ++++++++++++++++++- .../exercises/medium/fizzbuzz-2.test.js | 37 ++++++++----- .../medium/only-even-numbers.test.js | 29 ++++++++-- .../medium/reverse-concatenate.test.js | 43 +++++++++++++-- 15 files changed, 493 insertions(+), 82 deletions(-) diff --git a/2-write/1-function-design/exercises/easy/count-down.test.js b/2-write/1-function-design/exercises/easy/count-down.test.js index 98e9d0f..20ce1c5 100644 --- a/2-write/1-function-design/exercises/easy/count-down.test.js +++ b/2-write/1-function-design/exercises/easy/count-down.test.js @@ -1,5 +1,8 @@ // #todo +'use strict'; +// #todo + 'use strict'; /** @@ -10,6 +13,17 @@ */ // -------- your solutions -------- +function countdown(start = 0) { + if (!Number.isInteger(start) || start <= 0) { + throw new Error('Start must be an integer greater than 0.'); + } + + const result = []; + for (let i = start; i >= 0; i--) { + result.push(i); + } + return result; +} for (const solution of [secretSolution]) { // the main test suite for the function @@ -24,9 +38,37 @@ for (const solution of [secretSolution]) { expect(solution(1)).toEqual([1, 0]); }); // write at least 5 more tests ... - }); + it('for countdown to 10', () => { + expect(solution(10)).toEqual([10,9,8,7,6,5,4,3,2,1,0]); + }); + it('for countdown to 5', () => { + expect(solution(5)).toEqual([5,4,3,2,1,0]); + }); + it('for countdown 9', () => { + expect(solution(9)).toEqual([9,8,7,6,5,4,3,2,1,0]); + }); + it('Large start value -> [1000, 999, ..., 0]', () => { + const start = 1000; + const expected = Array.from({ length: start + 1 }, (_, i) => start - i); + expect(solution(start)).toEqual(expected); + }); + it('Negative start value throws RangeError', () => { + expect(() => solution(-5)).toThrow(RangeError); + }); + it('Fractional start value throws TypeError', () => { + expect(() => solution(2.5)).toThrow(TypeError); + }); + it('String start value throws TypeError', () => { + expect(() => solution('abc')).toThrow(TypeError); + }); + it('Empty parameter throws TypeError', () => { + expect(() => solution(null)).toThrow(TypeError); + }); + it('start is not an integer', () => { + expect(() => solution(3.14)).toThrow(Error); + }); +}); } - // minified solution for testing your tests // prettier-ignore -function secretSolution(a = 0) { if ("number" != typeof a) throw new TypeError("start is not a number"); if (!Number.isInteger(a)) throw new Error("start is not an integer"); if (0 > a) throw new RangeError("start is less than 0"); const b = []; for (let c = a; 0 <= c; c--)b.push(c); return b } +function secretSolution(a = 0) { if ("number" != typeof a) throw new TypeError("start is not a number"); if (!Number.isInteger(a)) throw new Error("start is not an integer"); if (0 > a) throw new RangeError("start is less than 0"); const b = []; for (let c = a; 0 <= c; c--) b.push(c);return b } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/easy/count-up.test.js b/2-write/1-function-design/exercises/easy/count-up.test.js index c2f1586..c4896fc 100644 --- a/2-write/1-function-design/exercises/easy/count-up.test.js +++ b/2-write/1-function-design/exercises/easy/count-up.test.js @@ -10,6 +10,25 @@ */ // -------- your solutions -------- +const countUp1 = (num = 0) => { + const arr = []; + for (let i = 0; i <= num; i++) { + arr.push(i); + } + + return arr; +}; + +const countUp2 = (num = 0) => { + const arr = []; + let i = 0; + while (i <= num) { + arr.push(i); + num--; + } + return arr; +}; + for (const solution of [secretSolution]) { // the main test suite for the function @@ -21,13 +40,29 @@ for (const solution of [secretSolution]) { it('0 -> [0]', () => { expect(solution(0)).toEqual([0]); }); - it('1 -> [0, 1]', () => { - expect(solution(1)).toEqual([0, 1]); - }); // write at least 5 more tests ... + it('5 -> [0, 1,2,3,4,5]', () => { + expect(solution(5)).toEqual([0,1,2,3,4,5]); + }); + + it('10 -> [0,1,2,3,4,5,6,7,8,9,10]', () => { + expect(solution(10)).toEqual([0,1,2,3,4,5,6,7,8,9,10]); + }); + it('throws an error if integer is negative', () => { + expect(() => solution(-5)).toThrow(RangeError); + }); + it('throws an error if parameter is not a number', () => { + expect(() => solution("hello")).toThrow(TypeError); + }); + it('Empty parameter throws TypeError', () => { + expect(() => solution(null)).toThrow(TypeError); + }); + it('Non-integer max value throws Error', () => { + expect(() => solution(3.14)).toThrow(Error); + }); }); } // minified solution for testing your tests // prettier-ignore -function secretSolution(a = 0) { if ("number" != typeof a) throw new TypeError("max is not a number"); if (!Number.isInteger(a)) throw new Error("max is not an integer"); if (0 > a) throw new RangeError("max is less than 0"); const b = []; for (let c = 0; c <= a; c++)b.push(c); return b } +function secretSolution(a = 0) { if ("number" != typeof a) throw new TypeError("max is not a number"); if (!Number.isInteger(a)) throw new Error("max is not an integer"); if (0 > a) throw new RangeError("max is less than 0"); const b = []; for (let c = 0; c <= a; c++)b.push(c); return b } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/easy/reverse-a-string.test.js b/2-write/1-function-design/exercises/easy/reverse-a-string.test.js index f74b041..9dfc13e 100644 --- a/2-write/1-function-design/exercises/easy/reverse-a-string.test.js +++ b/2-write/1-function-design/exercises/easy/reverse-a-string.test.js @@ -10,6 +10,10 @@ // -------- your solutions -------- +const reverseString = (str) => { + return str.split('').reverse().join(''); +}; + for (const solution of [secretSolution]) { // the main test suite for the function describe(solution.name + ': reverses a string', () => { @@ -23,6 +27,22 @@ for (const solution of [secretSolution]) { expect(solution('ASDF')).toEqual('FDSA'); }); // write at least 5 more tests ... + + it('a string with mixed cases and spaces', () => { + expect(solution('Hello World')).toEqual('dlroW olleH'); + }); + it('a string with special characters', () => { + expect(solution('!@#$%^&*()')).toEqual(')(*&^%$#@!'); + }); + it('a string with numbers', () => { + expect(solution('12345')).toEqual('54321'); + }); + it('a long string', () => { + expect(solution('abcdefghijklmnopqrstuvwxyz')).toEqual('zyxwvutsrqponmlkjihgfedcba'); + }); + it('a string with repeated characters', () => { + expect(solution('hellohello')).toEqual('olleholleh'); + }); }); } diff --git a/2-write/1-function-design/exercises/easy/reverse-and-case.test.js b/2-write/1-function-design/exercises/easy/reverse-and-case.test.js index bd04004..e36e56e 100644 --- a/2-write/1-function-design/exercises/easy/reverse-and-case.test.js +++ b/2-write/1-function-design/exercises/easy/reverse-and-case.test.js @@ -12,6 +12,19 @@ */ // -------- your solutions -------- +function reverseAndCasify(text = '', lowerCase = true) { + if (typeof text !== 'string') { + throw new TypeError('The text parameter must be a string.'); + } + + const reversedText = text.split('').reverse().join(''); + + if (lowerCase) { + return reversedText.toLowerCase(); + } else { + return reversedText.toUpperCase(); + } +} for (const solution of [secretSolution]) { describe( @@ -28,22 +41,55 @@ for (const solution of [secretSolution]) { // write the tests indicated by the comments describe('when set to lower case', () => { // when the text is an empty string - it(_, () => { - expect(solution(_, _)).toEqual(_); + it('empty string', () => { + expect(solution('', true)).toEqual(''); }); // when the text is all upper case + it('all uppercase', () => { + expect(solution('HELLO', true)).toEqual('olleh'); + }); // when the text is all lower case + it('all lowercase', () => { + expect(solution('hello', true)).toEqual('olleh'); + }); // when the text is mixed upper and lower case + it('mixed upper and lower case', () => { + expect(solution('HelloWorld', true)).toEqual('dlrowolleh'); + }); // when the text contains punctuation + it('the text containing punctuation', () => { + expect(solution('Hello, World!', true)).toEqual('!dlrow ,olleh'); + }); // when the text contains numbers + it('the text containing numbers', () => { + expect(solution('Hello123', true)).toEqual('321olleh'); + }); }); describe('when set to upper case', () => { // when the text is an empty string + it('empty string', () => { + expect(solution('', false)).toEqual(''); + }); // when the text is all upper case + it('text with all upper case', () => { + expect(solution('HELLO', false)).toEqual('OLLEH'); + }); // when the text is all lower case + it('text with all lower case', () => { + expect(solution('hello', false)).toEqual('OLLEH'); + }); // when the text is mixed upper and lower case + it('text with mixed case', () => { + expect(solution('HelloWorld', false)).toEqual('DLROWOLLEH'); + }); // when the text contains punctuation + it('text containing punctuation', () => { + expect(solution('Hello, World!', false)).toEqual('!DLROW ,OLLEH'); + }); // when the text contains numbers + it('text containing numbers', () => { + expect(solution('HelloWorld123', false)).toEqual('321DLROWOLLEH'); + }); }); } ); @@ -51,4 +97,4 @@ for (const solution of [secretSolution]) { // minified solution for testing your tests // prettier-ignore -function secretSolution(a = "", b = !0) { if ("string" != typeof a) { throw new TypeError("text is not a string"); } if ("boolean" != typeof b) { throw new TypeError("lowerCase is not a boolean"); } let c = ""; for (let d = a.length - 1; 0 <= d; d--)c += a[d]; let d = ""; return d = b ? c.toLowerCase() : c.toUpperCase(), d } +function secretSolution(a = "", b = !0) { if ("string" != typeof a) { throw new TypeError("text is not a string"); } if ("boolean" != typeof b) { throw new TypeError("lowerCase is not a boolean"); } let c = ""; for (let d = a.length - 1; 0 <= d; d--)c += a[d]; let d = ""; return d = b ? c.toLowerCase() : c.toUpperCase(), d } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/easy/set-the-case.test.js b/2-write/1-function-design/exercises/easy/set-the-case.test.js index 6d3762d..0880735 100644 --- a/2-write/1-function-design/exercises/easy/set-the-case.test.js +++ b/2-write/1-function-design/exercises/easy/set-the-case.test.js @@ -12,6 +12,18 @@ */ // -------- your solutions -------- +function casifyText(text = '', lowerCase = true) { + if (typeof text !== 'string') { + throw new TypeError('The text parameter must be a string.'); + } + + if (lowerCase) { + return text.toLowerCase(); + } else { + return text.toUpperCase(); + } +} + for (const solution of [secretSolution]) { describe(solution.name + ': sets a text to lower or upper case', () => { @@ -26,26 +38,59 @@ for (const solution of [secretSolution]) { // write the tests indicated by the comments describe('when set to lower case', () => { // when the text is an empty string - it(_, () => { - expect(solution(_, _)).toEqual(_); + it('for empty string', () => { + expect(solution('',true)).toEqual(''); }); // when the text is all upper case + it('for the text is all upper case', () => { + expect(solution('HELLO',true)).toEqual('hello'); + }); // when the text is all lower case + it('for the text is all lower case', () => { + expect(solution('hello',true)).toEqual('hello'); + }); // when the text is mixed upper and lower case + it('for the text is mixed case', () => { + expect(solution('HelloWorld',true)).toEqual('helloworld'); + }); // when the text contains punctuation + it('for the text containing punctuation', () => { + expect(solution('Hello, World!',true)).toEqual('hello, world!'); + }); // when the text contains numbers + it('for the text containing numbers', () => { + expect(solution('Pallavi123',true)).toEqual('pallavi123'); + }); }); describe('when set to upper case', () => { // when the text is an empty string + it('for empty string', () => { + expect(solution('',false)).toEqual(''); + }); // when the text is all upper case + it('for the text is all upper case', () => { + expect(solution('HELLO',false)).toEqual('HELLO'); + }); // when the text is all lower case + it('for the text is all lower case', () => { + expect(solution('hello',false)).toEqual('HELLO'); + }); // when the text is mixed upper and lower case + it('for the text is mixed case', () => { + expect(solution('HelloWorld',false)).toEqual('HELLOWORLD'); + }); // when the text contains punctuation + it('for the text containig punctuation', () => { + expect(solution('Hello, World!',false)).toEqual('HELLO, WORLD!'); + }); // when the text contains numbers + it('for the text containing numbers', () => { + expect(solution('Pallavi123',false)).toEqual('PALLAVI123'); + }); }); }); } // minified solution for testing your tests // prettier-ignore -function secretSolution(a = "", b = !0) { if ("string" != typeof a) { throw new TypeError("text is not a string"); } if ("boolean" != typeof b) { throw new TypeError("lowerCase is not a boolean"); } let c = ""; return c = b ? a.toLowerCase() : a.toUpperCase(), c } +function secretSolution(a = "", b = !0) { if ("string" != typeof a) { throw new TypeError("text is not a string"); } if ("boolean" != typeof b) { throw new TypeError("lowerCase is not a boolean"); } let c = ""; return c = b ? a.toLowerCase() : a.toUpperCase(), c } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/easy/testing-paths-if-else-if-else.test.js b/2-write/1-function-design/exercises/easy/testing-paths-if-else-if-else.test.js index c84c7fc..e8db25a 100644 --- a/2-write/1-function-design/exercises/easy/testing-paths-if-else-if-else.test.js +++ b/2-write/1-function-design/exercises/easy/testing-paths-if-else-if-else.test.js @@ -13,32 +13,47 @@ */ // -------- your solutions -------- +function compareValues(val1, val2) { + if (val1 === val2) { + return 'strictly equal'; + } else if (typeof val1 === typeof val2) { + return 'same type'; + } else { + return 'totally different'; + } +} for (const solution of [secretSolution]) { describe(solution.name + ': determines how similar two values are', () => { describe('when values are strictly equal', () => { it('two identical strings -> "strictly equal"', () => { - expect(solution('hello', 'hello')).toEqual(_); + expect(solution('hello', 'hello')).toEqual('strictly equal'); }); it('two identical numbers -> "strictly equal"', () => { + expect(solution(1, 1.0)).toEqual('strictly equal'); // 1, 1.0 }); - it('two identical booleans -> "strictly equal"', () => {}); + it('two identical booleans -> "strictly equal"', () => { + expect(solution(true, true)).toEqual('strictly equal'); + }); }); describe('when values have the same type', () => { it('two different strings -> "same type"', () => { - expect(_).toEqual('same type'); + expect(solution('hello', 'world')).toEqual('same type'); }); it('two different numbers -> "same type"', () => { - expect(_).toEqual(_); + expect(solution(12, 34)).toEqual('same type'); + }); + it('two different booleans -> "same type"', () => { + expect(solution(true, false)).toEqual('same type'); }); - it('two different booleans -> "same type"', () => {}); }); describe('when values are nothing alike', () => { it('values that are obviously different', () => { - _(_(null, 4))._(_); + expect(solution(null, 4)).toEqual('totally different'); }); it('values that can be confusing', () => { + expect(solution('4', 4)).toEqual('totally different'); // "4" and 4 }); }); @@ -47,4 +62,4 @@ for (const solution of [secretSolution]) { // minified solution for testing your tests // prettier-ignore -function secretSolution(a, b) { let c = ""; return c = a === b ? "strictly equal" : typeof a == typeof b ? "same type" : "totally different", c } +function secretSolution(a, b) { let c = ""; return c = a === b ? "strictly equal" : typeof a == typeof b ? "same type" : "totally different", c } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/easy/testing-paths-if-else.test.js b/2-write/1-function-design/exercises/easy/testing-paths-if-else.test.js index 7bb3b91..c480ab1 100644 --- a/2-write/1-function-design/exercises/easy/testing-paths-if-else.test.js +++ b/2-write/1-function-design/exercises/easy/testing-paths-if-else.test.js @@ -10,6 +10,14 @@ */ // -------- your solutions -------- +function isTruthy(value) { + if (value) { + return true; + } else { + return false; + } +} + for (const solution of [secretSolution]) { /* Execution Paths @@ -18,35 +26,35 @@ for (const solution of [secretSolution]) { describe(solution.name + ': determines if a value is truthy', () => { describe('solution can identify truthy values', () => { it('non-empty strings -> true', () => { - const actual = solution(_); + const actual = solution('Hello'); expect(actual).toEqual(true); }); it('numbers that are not 0 or NaN -> true', () => { - const actual = _; + const actual = solution(11); expect(actual).toEqual(true); }); it('true -> true', () => { - expect(solution(_)).toEqual(true); + expect(solution(true)).toEqual(true); }); }); describe('solution can identify falsy values', () => { - it('"" -> flase', () => { - _; + it('"" -> false', () => { + expect(solution('')).toEqual(false); }); it('0 -> false', () => { - _; + expect(solution(0)).toEqual(false); }); it('NaN -> false', () => { - _; + expect(solution(NaN)).toEqual(false); }); it('false -> false', () => { - _; + expect(solution(false)).toEqual(false); }); it('undefined -> false', () => { - _; + expect(solution(undefined)).toEqual(false); }); it('null -> false', () => { - _; + expect(solution(null)).toEqual(false); }); }); }); @@ -55,4 +63,4 @@ for (const solution of [secretSolution]) { // minified solution for testing your tests // prettier-ignore -function secretSolution(value) { return value ? true : false; } +function secretSolution(value) { return value ? true : false; } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/easy/testing-paths-sequential-conditionals.test.js b/2-write/1-function-design/exercises/easy/testing-paths-sequential-conditionals.test.js index 391094b..0a8669f 100644 --- a/2-write/1-function-design/exercises/easy/testing-paths-sequential-conditionals.test.js +++ b/2-write/1-function-design/exercises/easy/testing-paths-sequential-conditionals.test.js @@ -15,28 +15,35 @@ */ // -------- your solutions -------- +function booleanToBinaryString(a = false, b = false) { + return (a ? '1' : '0') + (b ? '1' : '0'); +} + for (const solution of [secretSolution]) { // this function only 4 possible combinations of arguments // it's possible test them all and have 100% confidence in the function describe(solution.name + ': converts two booleans to binary', () => { it('true, true --> "11"', () => { - const actual = solution(_, _); - expect(actual).toEqual(_); + const actual = solution(true, true); + expect(actual).toEqual('11'); }); it('true, false --> "10"', () => { - const actual = _; + const actual = solution(true, false); expect(actual).toEqual('10'); }); it('false, true --> "01"', () => { - const actual = _; - _; + const actual = solution(false, true); + expect(actual).toEqual('01'); + }); + it('false, false --> "00"', () => { + const actual = solution(false, false); + expect(actual).toEqual('00'); }); - it('_', () => {}); }); } // minified solution for testing your tests // prettier-ignore -function secretSolution(c = false, a = false) { if ("boolean" != typeof c) { throw new TypeError("a is not boolean"); } if ("boolean" != typeof a) { throw new TypeError("b is not boolean"); } let b = ""; return b += c ? "1" : "0", b += a ? "1" : "0", b } +function secretSolution(c = false, a = false) { if ("boolean" != typeof c) { throw new TypeError("a is not boolean"); } if ("boolean" != typeof a) { throw new TypeError("b is not boolean"); } let b = ""; return b += c ? "1" : "0", b += a ? "1" : "0", b } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/hard/numbery-numberify.test.js b/2-write/1-function-design/exercises/hard/numbery-numberify.test.js index d293ce7..72d4962 100644 --- a/2-write/1-function-design/exercises/hard/numbery-numberify.test.js +++ b/2-write/1-function-design/exercises/hard/numbery-numberify.test.js @@ -13,15 +13,51 @@ */ // -------- your solutions -------- + const numberyNumberify = (arr) => { + const isNotNaN = (entry) => { + return !Number.isNaN(entry); + }; + const castToNumber = (entry) => { + return Number(entry); + }; + const allValidNumbers = arr.map((item) => castToNumber(item)).filter((item) => isNotNaN(item)); + + return allValidNumbers; +}; + for (const solution of [secretSolution]) { - describe(solution.name + ': _', () => { - describe('_', () => { - it('_', () => {}); + describe(solution.name + ': converts string array to numbers', () => { + describe('converts array of strings to array of numbers', () => { + it('array of strings -> array of numbers', () => { + const input = ['1', '2', '3', '4', '5']; + const expected = [1, 2, 3, 4, 5]; + expect(numberyNumberify(input)).toEqual(expected); + }); + it('ignores non-numeric strings and returns only numbers', () => { + const input = ['1', 'a', '2', 'b', '3', 'c']; + const expected = [1, 2, 3]; + expect(numberyNumberify(input)).toEqual(expected); }); + it('returns an empty array if input array is empty', () => { + const input = []; + const expected = []; + expect(numberyNumberify(input)).toEqual(expected); + }); + it('converts negative numbers represented as strings to numbers', () => { + const input = ['-1', '-2', '-3']; + const expected = [-1, -2, -3]; + expect(numberyNumberify(input)).toEqual(expected); + }); + it('handles floating point numbers represented as strings', () => { + const input = ['1.5', '2.3', '3.7']; + const expected = [1.5, 2.3, 3.7]; + expect(numberyNumberify(input)).toEqual(expected); }); +}); +}); } // minified solution for testing your tests // prettier-ignore -function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arrayOfStrings is not an array"); } const b = a.some(a => "string" != typeof a); if (b) { throw new TypeError("arrayOfStrings contains non-strings"); } const c = a.map(a => +a), d = c.filter(a => !Number.isNaN(a)); return d } +function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arrayOfStrings is not an array"); } const b = a.some(a => "string" != typeof a); if (b) { throw new TypeError("arrayOfStrings contains non-strings"); } const c = a.map(a => +a), d = c.filter(a => !Number.isNaN(a)); return d } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/hard/passing-objects.test.js b/2-write/1-function-design/exercises/hard/passing-objects.test.js index 31c1321..a045f0a 100644 --- a/2-write/1-function-design/exercises/hard/passing-objects.test.js +++ b/2-write/1-function-design/exercises/hard/passing-objects.test.js @@ -11,21 +11,31 @@ */ // -------- your solutions -------- +function filterPassingObjects(arr) { + const passingObjects = []; + for (const obj of arr) { + if (obj.pass === true) { + passingObjects.push(obj); + } + } + return passingObjects; +} + for (const solution of [secretSolution]) { describe(solution.name + ': filters out non-passing objects', () => { describe('correctly filters an array', () => { it('an empty array returns an empty array', () => { - const actual = solution(_); - expect(_).toEqual(_); + const actual = solution([]); + expect(actual).toEqual([]); }); it('keeps all entries when all are passing', () => { - const actual = solution([{ pass: _ }, { pass: _ }]); + const actual = solution([{ pass: true }, { pass: true }]); expect(actual).toEqual([{ pass: true }, { pass: true }]); }); it('removes all entries when all are not passing', () => { - const actual = solution([{ pass: _ }, { pass: _ }]); - expect(actual).toEqual(_); + const actual = solution([{ pass: false }, { pass: false }]); + expect(actual).toEqual([]); }); it('removes only not-passing entries', () => { const actual = solution([ @@ -33,20 +43,20 @@ for (const solution of [secretSolution]) { { pass: false }, { pass: true }, ]); - expect(actual).toEqual([{ _: _ }, { _: _ }]); + expect(actual).toEqual([{ pass: true}, { pass: true}]); }); it('removes entries with a truthy, but not true, .pass value', () => { - const actual = solution([{ pass: 100 }, { pass: 'hello' }, { _: _ }]); + const actual = solution([{ pass: 100 }, { pass: 'hello' }, { pass: true }]); expect(actual).toEqual([{ pass: true }]); }); it('removes entries with no .pass property', () => { const actual = solution([ - { hello: _ }, - { bye: _ }, - { pass: _ }, - { passing: _ }, + { hello: 'test'}, + { bye: 'test' }, + { pass: true }, + { passing: 'testing'}, ]); - expect(actual)._.deep._([{ pass: true }]); + expect(actual).toEqual([{ pass: true }]); }); }); describe('does not modify the argument', () => { @@ -59,7 +69,7 @@ for (const solution of [secretSolution]) { it('does not modify the argument', () => { const arg = [{ pass: true }, { pass: false }, { hello: 'good bye' }]; solution(arg); - expect(arg).toEqual([{ _: _ }, { _: _ }, { _: _ }]); + expect(arg).toEqual([{ pass: true }, { pass: false }, { hello: 'good bye' }]); }); }); }); @@ -67,4 +77,4 @@ for (const solution of [secretSolution]) { // minified solution for testing your tests // prettier-ignore -function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arr is not an array"); } if (!a.every(a => Object(a) === a)) { throw new TypeError("arr is not an array of objects"); } const b = a.filter(a => !0 === a.pass); return b } +function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arr is not an array"); } if (!a.every(a => Object(a) === a)) { throw new TypeError("arr is not an array of objects"); } const b = a.filter(a => !0 === a.pass); return b } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/hard/sum-numbery.test.js b/2-write/1-function-design/exercises/hard/sum-numbery.test.js index d24931a..4eb2dcd 100644 --- a/2-write/1-function-design/exercises/hard/sum-numbery.test.js +++ b/2-write/1-function-design/exercises/hard/sum-numbery.test.js @@ -11,6 +11,17 @@ */ // -------- your solutions -------- + function sumNumberyStrings(arr) { + let sum = 0; + for (const str of arr) { + const num = Number(str); + if (!isNaN(num)) { + sum += num; + } + } + return sum; +} + const mapFilterReduce = (arr) => { // these work, you need to pass them to the right array methods @@ -19,7 +30,10 @@ const mapFilterReduce = (arr) => { const castToNumber = (entry) => Number(entry); // fill in the array methods and pass in the correct logic - const sumOfNumberies = arr._(_)._(_)._(_, _); + const sumOfNumberies = arr + .map(castToNumber) // convert each string to a number + .filter(isNotNaN) // filter out non-numeric values + .reduce(sumNumbers, 0); //sum the remaining values, starting from 0 return sumOfNumberies; }; @@ -30,9 +44,22 @@ for (const solution of [ secretSolution, // mapFilterReduce, ]) { - describe(solution.name + ': _', () => { - describe('_', () => { - it('_', () => {}); + describe(solution.name + ': sumNumberyStrings', () => { + describe('checking different test cases', () => { + it('returns 0 for an empty array', () => { + const result = sumNumberyStrings([]); + expect(result).toBe(0); + }); + it('returns 0 if there is no numbery strings', () => { + const arr = ['a', 'b', 'c'] + const result = sumNumberyStrings(arr); + expect(result).toBe(0); + }); + it('returns sum of the numbery strings', () => { + const arr = [ 56, 'a', 8, 'b', 98, 5, 'c'] + const result = sumNumberyStrings(arr); + expect(result).toBe(56 + 8 + 98 + 5); + }); }); }); } @@ -40,4 +67,4 @@ for (const solution of [ // minified solution for testing your tests // prettier-ignore -function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arr is not an array"); } const b = a.some(a => "string" != typeof a); if (b) { throw new TypeError("arr contains non-strings"); } const c = a.map(a => +a).filter(a => !Number.isNaN(a)).reduce((a, b) => a + b, 0); return c } +function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arr is not an array"); } const b = a.some(a => "string" != typeof a); if (b) { throw new TypeError("arr contains non-strings"); } const c = a.map(a => +a).filter(a => !Number.isNaN(a)).reduce((a, b) => a + b, 0); return c } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/medium/fizzbuzz-1.test.js b/2-write/1-function-design/exercises/medium/fizzbuzz-1.test.js index 1f67956..6a73c99 100644 --- a/2-write/1-function-design/exercises/medium/fizzbuzz-1.test.js +++ b/2-write/1-function-design/exercises/medium/fizzbuzz-1.test.js @@ -14,6 +14,21 @@ */ // -------- your solutions -------- +function fizzBuzz(num =0){ + if(!Number.isInteger(num) || num < 0){ + throw new Error('num must be a non-negative integer.'); + } + if (num % 3 === 0 && num % 5 === 0){ + return 'fizzbuzz'; + } else if (num % 3 === 0){ + return 'fizz'; + } else if ( num % 5 === 0){ + return 'buzz'; + } else { + return num + } +} + for (const solution of [secretSolution]) { describe(solution.name + ': fizbuzzish', () => { @@ -30,6 +45,15 @@ for (const solution of [secretSolution]) { expect(solution(2)).toEqual(2); }); // write more tests in this category + it('7 -> 7', () => { + expect(solution(7)).toEqual(7); + }); + it('11 -> 11', () => { + expect(solution(11)).toEqual(11); + }); + it('17 -> 17', () => { + expect(solution(17)).toEqual(17); + }); }); describe('only divisible by only 3', () => { @@ -41,6 +65,15 @@ for (const solution of [secretSolution]) { expect(solution(6)).toEqual(expectedValue); }); // write more tests in this category + it('21 -> "fizz"', () => { + expect(solution(21)).toEqual(expectedValue); + }); + it('63 -> "fizz"', () => { + expect(solution(63)).toEqual(expectedValue); + }); + it('33 -> "fizz"', () => { + expect(solution(33)).toEqual(expectedValue); + }); }); describe('only divisible by only 5', () => { @@ -52,6 +85,15 @@ for (const solution of [secretSolution]) { expect(solution(10)).toEqual(expectedValue); }); // write more tests in this category + it('55 -> "buzz"', () => { + expect(solution(55)).toEqual(expectedValue); + }); + it('25 -> "buzz"', () => { + expect(solution(25)).toEqual(expectedValue); + }); + it('50 -> "buzz"', () => { + expect(solution(50)).toEqual(expectedValue); + }); }); describe('divisible by 5 and 3', () => { @@ -63,10 +105,19 @@ for (const solution of [secretSolution]) { expect(solution(30)).toEqual(expectedValue); }); // write more tests in this category + it('45-> "fizz"', () => { + expect(solution(45)).toEqual(expectedValue); + }); + it('90 -> "fizz"', () => { + expect(solution(90)).toEqual(expectedValue); + }); + it('60 -> "fizz"', () => { + expect(solution(60)).toEqual(expectedValue); + }); }); }); } // minified solution for testing your tests // prettier-ignore -function secretSolution(a = 0) { if ("number" != typeof a) { throw new TypeError("num is not a number"); } if (0 > a) { throw new RangeError("num is less than 0"); } if (!Number.isInteger(a)) { throw new RangeError("num is not an integer"); } return 0 == a % 3 && 0 == a % 5 ? "fizzbuzz" : 0 == a % 3 ? "fizz" : 0 == a % 5 ? "buzz" : a } +function secretSolution(a = 0) { if ("number" != typeof a) { throw new TypeError("num is not a number"); } if (0 > a) { throw new RangeError("num is less than 0"); } if (!Number.isInteger(a)) { throw new RangeError("num is not an integer"); } return 0 == a % 3 && 0 == a % 5 ? "fizzbuzz" : 0 == a % 3 ? "fizz" : 0 == a % 5 ? "buzz" : a } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/medium/fizzbuzz-2.test.js b/2-write/1-function-design/exercises/medium/fizzbuzz-2.test.js index 9fb5c99..ea9208e 100644 --- a/2-write/1-function-design/exercises/medium/fizzbuzz-2.test.js +++ b/2-write/1-function-design/exercises/medium/fizzbuzz-2.test.js @@ -16,10 +16,10 @@ // -------- your solutions -------- const whileLoop = (max) => { - let countUp = _; + let countUp = 0; const result = []; - while (_) { - const nextEntry = countUp % 15 === 0 ? '_' : _ ? 'buzz' : _ ? 'fizz' : _; + while (countUp < max) { + const nextEntry = countUp % 15 === 0 ? 'fixxbuzz' : countUp % 3 === 0 ? 'buzz' : countUp % 5 === 0 ? 'fizz' : countUp; result.push(nextEntry); } return result; @@ -29,8 +29,8 @@ const whileLoop = (max) => { */ const oneLineforLoop = (max) => { const result = []; - for (let i = 0; i < _; ) - result._((++i % _ ? '' : '_') + (i % _ ? '' : '_') || i); + for (let i = 0; i <= max; ) + result.push((++i % 3 ? '' : 'fizz') + (i % 5 ? '' : 'buzz') || i); return result; // https://codeburst.io/javascript-breaking-down-the-shortest-possible-fizzbuzz-answer-94a0ad9d128a @@ -39,16 +39,16 @@ const oneLineforLoop = (max) => { /* describe this solution's strategy */ const manySmallFunctions = (max) => { - const threeDivides = (n) => n % _ === 0; - const fiveDivides = (n) => n % _ === 0; - const fifteenDivides = (n) => n % _ === 0; + const threeDivides = (n) => n % 3 === 0; + const fiveDivides = (n) => n % 5 === 0; + const fifteenDivides = (n) => n % 15 === 0; const fizzbuzzOrNumber = (num) => { - if (_) { + if (fifteenDivides) { return 'fizzbuzz'; - } else if (_) { + } else if (threeDivides) { return 'fizz'; - } else if (_) { + } else if (fiveDivides) { return 'buzz'; } else { return num; @@ -75,18 +75,27 @@ for (const solution of [ expect(solution(3)).toEqual(['fizzbuzz', 1, 2]); }); // write more of these + it('6 should return an array with the first 3 values', () => { + expect(solution(6)).toEqual(['fizzbuzz', 1, 2, 'fizz', 4, 'buzz']); + }); }); describe('numbers divisible by neither 3 nor 5', () => { it('4 should return an array with the first 4 values', () => { expect(solution(4)).toEqual(['fizzbuzz', 1, 2, 'fizz']); }); // write more of these + it('7 should return an array with the first 4 values', () => { + expect(solution(7)).toEqual(['fizzbuzz', 1, 2, 'fizz', 4, 'buzz', 'fizz']); + }); }); describe('numbers divisible by 5', () => { it('5 should return an array with the first 5 values', () => { expect(solution(5)).toEqual(['fizzbuzz', 1, 2, 'fizz', 4]); }); // write more of these + it('10 should return an array with the first 5 values', () => { + expect(solution(10)).toEqual(['fizzbuzz', 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz']); + }); }); describe('numbers divisible by 3 and 5', () => { it('15 should return an array with the first 15 values', () => { @@ -109,6 +118,10 @@ for (const solution of [ ]); }); // write more of these + it('30 should return an array with the first 15 values', () => { + expect(solution(30)).toEqual([ + 'fizzbuzz', 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16, 17, 'fizz', 19, 'buzz', 'fizz', 22, 23, 'fizz', 'buzz', 26, 'fizz', 28, 29]); + }); }); }); } @@ -116,4 +129,4 @@ for (const solution of [ // minified solution for testing your tests // prettier-ignore -function secretSolution(a) { if ("number" != typeof a) { throw new TypeError("max is not a number"); } if (0 > a) { throw new RangeError("max is less than 0"); } if (!Number.isInteger(a)) { throw new RangeError("max is not an integer"); } const b = []; for (let c = 0; c < a; c++) { 0 == c % 3 && 0 == c % 5 ? b.push("fizzbuzz") : 0 == c % 3 ? b.push("fizz") : 0 == c % 5 ? b.push("buzz") : b.push(c); } return b; } +function secretSolution(a) { if ("number" != typeof a) { throw new TypeError("max is not a number"); } if (0 > a) { throw new RangeError("max is less than 0"); } if (!Number.isInteger(a)) { throw new RangeError("max is not an integer"); } const b = []; for (let c = 0; c < a; c++) { 0 == c % 3 && 0 == c % 5 ? b.push("fizzbuzz") : 0 == c % 3 ? b.push("fizz") : 0 == c % 5 ? b.push("buzz") : b.push(c); } return b; } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/medium/only-even-numbers.test.js b/2-write/1-function-design/exercises/medium/only-even-numbers.test.js index 5c557f6..2cfcccb 100644 --- a/2-write/1-function-design/exercises/medium/only-even-numbers.test.js +++ b/2-write/1-function-design/exercises/medium/only-even-numbers.test.js @@ -10,15 +10,36 @@ */ // -------- your solutions -------- +function removeOddNumbers(arrayOfNumbers) { + // Filter out odd numbers from the array + const evenNumbers = arrayOfNumbers.filter(number => number % 2 === 0); + + // Return the new array with only even numbers + return evenNumbers; +} for (const solution of [secretSolution]) { - describe(solution.name + ': _', () => { - describe('_', () => { - it('_', () => {}); + describe(solution.name + ': only even numbers', () => { + describe('when there are only odd numbers in the array', () => { + it('should return an empty array', () => { + const input = [1, 3, 5, 7]; + const result = solution(input); + expect(result).toEqual([]); + }); + it('when there are only even numbers in the array should return same array', () => { + const input = [2, 4, 6, 8]; + const result = solution(input); + expect(result).toEqual([2, 4, 6, 8]); + }); + it('when both even and odd numbers in the array should return an array with only even numbers', () => { + const input = [1, 2, 3, 4, 5, 6]; + const result = solution(input); + expect(result).toEqual([2, 4, 6]); }); }); +}); } // minified solution for testing your tests // prettier-ignore -function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arrayOfNumbers is not an array"); } const b = a.some(a => "number" != typeof a); if (b) { throw new TypeError("arrayOfNumbers does not contain only numbers"); } const c = a.filter(a => 0 == a % 2); return c } +function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arrayOfNumbers is not an array"); } const b = a.some(a => "number" != typeof a); if (b) { throw new TypeError("arrayOfNumbers does not contain only numbers"); } const c = a.filter(a => 0 == a % 2); return c } \ No newline at end of file diff --git a/2-write/1-function-design/exercises/medium/reverse-concatenate.test.js b/2-write/1-function-design/exercises/medium/reverse-concatenate.test.js index f0c1d20..b1846b6 100644 --- a/2-write/1-function-design/exercises/medium/reverse-concatenate.test.js +++ b/2-write/1-function-design/exercises/medium/reverse-concatenate.test.js @@ -10,11 +10,46 @@ */ // -------- your solutions -------- +function reverseAndCombine(arrayOfStrings) { + // Create a copy of the original array to avoid modifying it + const reversedArray = arrayOfStrings.slice().reverse(); + // Join the strings in the reversed array + return reversedArray.join(''); +} for (const solution of [secretSolution]) { - describe(solution.name + ': _', () => { - describe('_', () => { - it('_', () => {}); + describe(solution.name + ': reverse and combine', () => { + describe('test cases', () => { + it('reverses and combine an array of strings', () => { + const inputArray = ['hello', 'world', '!']; + const expectedResult = '!worldhello'; + expect(solution(inputArray)).toEqual(expectedResult); + }); + + it('does not modify the original array', () => { + const inputArray = ['hello', 'world', '!']; + const copiedArray = inputArray.slice(); // Create a copy of the original array + solution(inputArray); // Call the function + expect(inputArray).toEqual(copiedArray); // Check if the original array is unchanged + }); + + it('works with an empty array', () => { + const inputArray = []; + const expectedResult = ''; + expect(solution(inputArray)).toEqual(expectedResult); + }); + + it('works with an array containing a single string', () => { + const inputArray = ['hello']; + const expectedResult = 'hello'; + expect(solution(inputArray)).toEqual(expectedResult); + }); + + it('works with an array containing multiple empty strings', () => { + const inputArray = ['', '', '']; + const expectedResult = ''; + expect(solution(inputArray)).toEqual(expectedResult); + }); }); }); } @@ -22,4 +57,4 @@ for (const solution of [secretSolution]) { // minified solution for testing your tests // prettier-ignore -function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arrayOfStrings is not an array"); } const b = a.some(a => "string" != typeof a); if (b) { throw new TypeError("arrayOfStrings does not contain only strings"); } return [...a].reverse().reduce((a, b) => a + b, "") } +function secretSolution(a) { if (!Array.isArray(a)) { throw new TypeError("arrayOfStrings is not an array"); } const b = a.some(a => "string" != typeof a); if (b) { throw new TypeError("arrayOfStrings does not contain only strings"); } return [...a].reverse().reduce((a, b) => a + b, "") } \ No newline at end of file