8000 Added test cases · MatMammen/javascript-exercises@6012c3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6012c3a

Browse files
Added test cases
1 parent f72bdd2 commit 6012c3a

File tree

17 files changed

+452
-0
lines changed

17 files changed

+452
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

02_calculator/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The goal for this exercise is to create a calculator that does the following:
2+
3+
add, subtract, get the sum, multiply, get the power, and find the factorial
4+
5+
In order to do this please fill out each function with your solution. Make sure to return the value so you can test it in Jasmine! To see the expected value
6+
take a look at the spec file that houses the Jasmine test cases.

02_calculator/calculator.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function add () {
2+
3+
}
4+
5+
function subtract () {
6+
7+
}
8+
9+
function sum () {
10+
11+
}
12+
13+
function multiply () {
14+
15+
}
16+
17+
function power() {
18+
19+
}
20+
21+
function factorial() {
22+
23+
}
24+
25+
module.exports = {
26+
add,
27+
subtract,
28+
sum,
29+
multiply,
30+
power,
31+
factorial
32+
}

02_calculator/calculator.spec.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var calculator = require ('./calculator.js');
2+
3+
describe('add', function() {
4+
it('adds 0 and 0', function() {
5+
expect(calculator.add(0,0)).toEqual(0);
6+
});
7+
8+
it('adds 2 and 2', function() {
9+
expect(calculator.add(2,2)).toEqual(4);
10+
});
11+
12+
it('adds positive numbers', function() {
13+
expect(calculator.add(2,6)).toEqual(8);
14+
});
15+
});
16+
17+
describe('subtract', function() {
18+
it('subtracts numbers', function() {
19+
expect(calculator.subtract(10,4)).toEqual(6);
20+
});
21+
});
22+
23+
describe('sum', function() {
24+
it('computes the sum of an empty array', function() {
25+
expect(calculator.sum([])).toEqual(0);
26+
});
27+
28+
it('computes the sum of an array of one number', function() {
29+
expect(calculator.sum([7])).toEqual(7);
30+
});
31+
32+
it('computes the sum of an array of one number', function() {
33+
expect(calculator.sum([7,11])).toEqual(18);
34+
});
35+
36+
it('computes the sum of an array of many numbers', function() {
37+
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
38+
});
39+
});
40+
41+
describe('multiply', function() {
42+
it('multiplies two numbers', function() {
43+
expect(calculator.multiply([2,4])).toEqual(8);
44+
});
45+
46+
it('multiplies several numbers', function() {
47+
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
48+
});
49+
});
50+
51+
describe('power', function() {
52+
it('raises one number to the power of another number', function() {
53+
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
54+
});
55+
});
56+
57+
describe('factorial', function() {
58+
it('computes the factorial of 0', function() {
59+
expect(calculator.factorial(0)).toEqual(0);
60+
});
61+
62+
it('computes the factorial of 1', function() {
63+
expect(calculator.factorial(1)).toEqual(1);
64+
});
65+
66+
it('computes the factorial of 2', function() {
67+
expect(calculator.factorial(2)).toEqual(2);
68+
});
69+
70+
it('computes the factorial of 5', function() {
71+
expect(calculator.factorial(5)).toEqual(120);
72+
});
73+
74+
it('computes the factorial of 10', function() {
75+
expect(calculator.factorial(10)).toEqual(3628800);
76+
});
77+
});

03_simon_says/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This exercise is meant to (loosely) simulate the game Simon Says. The goal of this program in order is:
2+
3+
Echo the value given,
4+
5+
Capitalize the value given,
6+
7+
Repeat the value given,
8+
9+
Return a piece of the value given,
10+
11+
Return the first word of the value given,
12+
13+
Create a title with the value given
14+
15+
See Jasmine test cases for expected results.

03_simon_says/simonSays.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function echo() {
2+
3+
}
4+
5+
function shout() {
6+
7+
}
8+
9+
function repeat() {
10+
11+
}
12+
13+
function pieceOfWord() {
14+
15+
}
16+
17+
function firstWord() {
18+
19+
}
20+
21+
function capitalize(word) {
22+
return word.charAt(0).toUpperCase() + word.slice(1);
23+
// This function just capitalizes the word given to it, use in conjunction with titleCreator
24+
}
25+
26+
function titleCreator() {
27+
28+
}
29+
30+
module.exports = {
31+
echo,
32+
shout,
33+
repeat,
34+
pieceOfWord,
35+
firstWord,
36+
titleCreator
37+
}

03_simon_says/simonSays.spec.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var simon = require ('./simonSays.js');
2+
3+
describe('Simon says', function() {
4+
describe('echo', function() {
5+
it('should echo hello', function() {
6+
expect(simon.echo("hello")).toEqual("hello");
7+
});
8+
9+
it('should echo bye', function() {
10+
expect(simon.echo("bye")).toEqual("bye")
11+
});
12+
});
13+
14+
describe('shout', function() {
15+
it('should shout hello', function() {
16+
expect(simon.shout("hello")).toEqual("HELLO");
17+
});
18+
19+
it('should shout multiple words', function() {
F438
20+
expect(simon.shout("hello world")).toEqual("HELLO WORLD");
21+
});
22+
});
23+
24+
describe('repeat', function() {
25+
it('should repeat', function() {
26+
expect(simon.repeat("hello")).toEqual("hello hello");
27+
});
28+
29+
it('should repeat a number of times', function() {
30+
expect(simon.repeat("hello",3)).toEqual("hello hello hello");
31+
});
32+
});
33+
34+
describe('pieceOfWord', function() {
35+
it('returns the first letter', function() {
36+
expect(simon.pieceOfWord("hello", 1)).toEqual("h");
37+
});
38+
39+
it('returns the first two letters', function() {
40+
expect(simon.pieceOfWord("Bob", 2)).toEqual("Bo");
41+
});
42+
43+
it('returns the first several letters', function() {
44+
var s = "abcdefg";
45+
expect(simon.pieceOfWord(s, 1)).toEqual("a");
46+
expect(simon.pieceOfWord(s, 2)).toEqual("ab");
47+
expect(simon.pieceOfWord(s, 3)).toEqual("abc");
48+
});
49+
});
50+
51+
describe('firstWord', function() {
52+
it('tells us the first word of "Hello World" is "Hello"', function() {
53+
expect(simon.firstWord("Hello World")).toEqual("Hello");
54+
});
55+
56+
it('tells us the first word of "oh dear" is "oh"', function() {
57+
expect(simon.firstWord("oh dear")).toEqual("oh");
58+
});
59+
});
60+
61+
describe('titleCreator', function() {
62+
it('capitalizes a word', function() {
63+
expect(simon.titleCreator("jaws")).toEqual("Jaws");
64+
});
65+
66+
it('capitalizes every word (aka title case)', function() {
67+
expect(simon.titleCreator("david copperfield")).toEqual("David Copperfield");
68+
});
69+
70+
it("doesn't capitalize 'little words' in a title", function() {
71+
expect(simon.titleCreator("war and peace")).toEqual("War and Peace");
72+
});
73+
74+
it('does capitalize "little words" at the start of a title', function() {
75+
expect(simon.titleCreator("the bridge over the river kwai")).toEqual("The Bridge over the River Kwai");
76+
});
77+
});
78+
});

04_pig_latin/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Pig Latin is a children's language that is intended to be confusing when spoken quickly. Your job for this exercise is to create a solution that takes the words given and
2+
turns them into pig latin. Please see the following wikipedia page for details regarding the rules of Pig Latin:
3+
4+
https://en.wikipedia.org/wiki/Pig_Latin
5+
6+
The rules section will give the rules and the examples that are required to complete this exercise.

04_pig_latin/pigLatin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function translate() {
2+
// body...
3+
}
4+
5+
6+
module.exports = {
7+
translate
8+
}
9+

04_pig_latin/pigLatin.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Topics
2+
3+
// * modules
4+
// * strings
5+
6+
// Pig Latin
7+
8+
// Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below) but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.
9+
10+
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
11+
12+
// Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.
13+
14+
// (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
15+
16+
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.
17+
18+
var pigLatin = require("./pigLatin.js");
19+
20+
describe('#translate', function() {
21+
it('translates a word beginning with a vowel', function() {
22+
s = pigLatin.translate("apple");
23+
expect(s).toEqual('appleay');
24+
});
25+
26+
it('translates a word beginning with a consonant', function() {
27+
s = pigLatin.translate("banana");
28+
expect(s).toEqual("ananabay");
29+
});
30+
31+
it('translates a word beginning with two consonants', function() {
32+
s = pigLatin.translate("cherry");
33+
expect(s).toEqual('errychay');
34+
});
35+
36+
it('translates two words', function() {
37+
s = pigLatin.translate("eat pie");
38+
expect(s).toEqual('eatay iepay');
39+
});
40+
41+
it('translates a word beginning with three consonants', function() {
42+
expect(pigLatin.translate("three")).toEqual("eethray");
43+
});
44+
45+
it('counts "sch" as a single phoneme', function() {
46+
s = pigLatin.translate("school");
47+
expect(s).toEqual("oolschay");
48+
});
49+
50+
it('counts "qu" as a single phoneme', function() {
51+
s = pigLatin.translate("quiet");
52+
expect(s).toEqual("ietquay");
53+
});
54+
55+
it('counts "qu" as a consonant even when its preceded by a consonant', function() {
56+
s = pigLatin.translate("square");
57+
expect(s).toEqual("aresquay");
58+
});
59+
60+
it('translates many words', function() {
61+
s = pigLatin.translate("the quick brown fox");
62+
expect(s).toEqual("ethay ickquay ownbray oxfay");
63+
});
64+
});

0 commit comments

Comments
 (0)
0