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
21 changes: 18 additions & 3 deletions 2-write/1-function-design/exercises/easy/count-down.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
*/

// -------- your solutions --------
function countDown(start = 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 +31,17 @@ 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]);
});
});
}

// 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 }
27 changes: 24 additions & 3 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,17 @@
*/

// -------- your solutions --------
function countUp(max = 0) {
if (max <= 0 || !Number.isInteger(max)) {
throw new TypeError('Max must be a positive integer.');
}

const result = [];
for (let i = 0; i <= max; i++) {
result.push(i);
}
return result;
}

for (const solution of [secretSolution]) {
// the main test suite for the function
Expand All @@ -21,10 +32,20 @@ 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);
});
});
}

Expand Down
12 changes: 12 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 @@ -9,6 +9,9 @@
*/

// -------- your solutions --------
const reverseString = (str) => {
return str.split('').reverse().join('');
};

for (const solution of [secretSolution]) {
// the main test suite for the function
Expand All @@ -23,6 +26,15 @@ for (const solution of [secretSolution]) {
expect(solution('ASDF')).toEqual('FDSA');
});
// write at least 5 more tests ...
it('a string is a number', () => {
expect(solution('123')).toEqual('321');
});
it('a string is a number', () => {
expect(solution('!@#$%')).toEqual('%$#@!');
});
it('is a string ', () => {
expect(solution('hello world')).toEqual('dlrow olleh');
});
});
}

Expand Down
50 changes: 48 additions & 2 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,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');
});
});
}
);
Expand Down
49 changes: 47 additions & 2 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,22 +38,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('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');
});
});
});
}
Expand Down
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
*/

// -------- your solutions --------
function isTruthy(value) {
if (value) {
return true;
} else {
return false;
}
}


for (const solution of [secretSolution]) {
/* Execution Paths
Expand All @@ -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);
});
});
});
Expand Down
Loading