Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions 2-write/1-function-design/exercises/easy/count-down.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// #todo

'use strict';
// #todo

'use strict';

/**
Expand All @@ -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
Expand All @@ -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 }
43 changes: 39 additions & 4 deletions 2-write/1-function-design/exercises/easy/count-up.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
20 changes: 20 additions & 0 deletions 2-write/1-function-design/exercises/easy/reverse-a-string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});
}

Expand Down
52 changes: 49 additions & 3 deletions 2-write/1-function-design/exercises/easy/reverse-and-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -28,27 +41,60 @@ 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');
});
});
}
);
}

// 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 }
51 changes: 48 additions & 3 deletions 2-write/1-function-design/exercises/easy/set-the-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
Expand All @@ -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 }
Loading