Skip to content

Commit 4da2775

Browse files
committed
Add solutions to calculator, palindrome and caesar exercises
1 parent 3786bdd commit 4da2775

7 files changed

Lines changed: 138 additions & 102 deletions

File tree

caesar/caesar.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
const caesar = function() {
1+
const caesar = function (word, num) {
2+
const arr = [];
3+
for (let i = 0; i < word.length; i++) {
4+
arr.push(word.charCodeAt(i));
5+
}
6+
const shifted = arr.map((x) => {
7+
if (x >= 97 && x <= 122) {
8+
//if num is positive; (26 + num % 26) will be equal to num
9+
// if num is negative; (26 + num % 26) will be the same as subtracting num from 26
10+
const shift = (x - 97 + (26 + (num % 26))) % 26;
11+
return String.fromCharCode(shift + 97);
12+
} else if (x >= 65 && x <= 90) {
13+
const shift = (x - 65 + (26 + (num % 26))) % 26;
14+
return String.fromCharCode(shift + 65);
15+
} else {
16+
return String.fromCharCode(x);
17+
}
18+
});
219

3-
}
20+
return shifted.join("");
21+
};
422

5-
module.exports = caesar
23+
module.exports = caesar;

caesar/caesar.spec.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
const caesar = require('./caesar')
1+
const caesar = require("./caesar");
22

3-
describe('caesar', function() {
4-
it('works with single letters', function() {
5-
expect(caesar('A', 1)).toEqual('B');
6-
});
7-
xit('works with words', function() {
8-
expect(caesar('Aaa', 1)).toEqual('Bbb');
9-
});
10-
xit('works with phrases', function() {
11-
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
12-
});
13-
xit('works with negative shift', function() {
14-
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
15-
});
16-
xit('wraps', function() {
17-
expect(caesar('Z', 1)).toEqual('A');
18-
});
19-
xit('works with large shift factors', function() {
20-
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
21-
});
22-
xit('works with large negative shift factors', function() {
23-
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
24-
});
3+
describe("caesar", function () {
4+
it("works with single letters", function () {
5+
expect(caesar("A", 1)).toEqual("B");
6+
});
7+
it("works with words", function () {
8+
expect(caesar("Aaa", 1)).toEqual("Bbb");
9+
});
10+
it("works with phrases", function () {
11+
expect(caesar("Hello, World!", 5)).toEqual("Mjqqt, Btwqi!");
12+
});
13+
it("works with negative shift", function () {
14+
expect(caesar("Mjqqt, Btwqi!", -5)).toEqual("Hello, World!");
15+
});
16+
it("wraps", function () {
17+
expect(caesar("Z", 1)).toEqual("A");
18+
});
19+
it("works with large shift factors", function () {
20+
expect(caesar("Hello, World!", 75)).toEqual("Ebiil, Tloia!");
21+
});
22+
it("works with large negative shift factors", function () {
23+
expect(caesar("Hello, World!", -29)).toEqual("Ebiil, Tloia!");
24+
});
2525
});

calculator/calculator.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1-
function add () {
2-
1+
function add(a, b) {
2+
return a + b;
33
}
44

5-
function subtract () {
6-
5+
function subtract(a, b) {
6+
return a - b;
77
}
88

9-
function sum () {
10-
9+
function sum(arr) {
10+
return arr.length === 0 ? 0 : arr.reduce((acc, val) => (acc += val));
1111
}
1212

13-
function multiply () {
14-
13+
function multiply(arr) {
14+
return arr.reduce((acc, val) => (acc *= val));
1515
}
1616

17-
function power() {
18-
17+
function power(a, b) {
18+
let expo = 1;
19+
for (let i = 0; i < b; i++) {
20+
expo *= a;
21+
}
22+
return expo;
1923
}
2024

21-
function factorial() {
22-
25+
function factorial(n) {
26+
let product = n;
27+
if (n === 0) {
28+
return 1;
29+
} else {
30+
for (let i = n - 1; i > 0; i--) {
31+
product *= i;
32+
}
33+
}
34+
return product;
2335
}
2436

2537
module.exports = {
2638
add,
2739
subtract,
2840
sum,
2941
multiply,
30-
power,
31-
factorial
32-
}
42+
power,
43+
factorial,
44+
};

calculator/calculator.spec.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
1-
const calculator = require ('./calculator.js');
1+
const calculator = require("./calculator.js");
22

3-
describe('add', function() {
4-
it('adds 0 and 0', function() {
5-
expect(calculator.add(0,0)).toEqual(0);
3+
describe("add", function () {
4+
it("adds 0 and 0", function () {
5+
expect(calculator.add(0, 0)).toEqual(0);
66
});
77

8-
xit('adds 2 and 2', function() {
9-
expect(calculator.add(2,2)).toEqual(4);
8+
it("adds 2 and 2", function () {
9+
expect(calculator.add(2, 2)).toEqual(4);
1010
});
1111

12-
xit('adds positive numbers', function() {
13-
expect(calculator.add(2,6)).toEqual(8);
12+
it("adds positive numbers", function () {
13+
expect(calculator.add(2, 6)).toEqual(8);
1414
});
1515
});
1616

17-
describe('subtract', function() {
18-
xit('subtracts numbers', function() {
19-
expect(calculator.subtract(10,4)).toEqual(6);
17+
describe("subtract", function () {
18+
it("subtracts numbers", function () {
19+
expect(calculator.subtract(10, 4)).toEqual(6);
2020
});
2121
});
2222

23-
describe('sum', function() {
24-
xit('computes the sum of an empty array', function() {
23+
describe("sum", function () {
24+
it("computes the sum of an empty array", function () {
2525
expect(calculator.sum([])).toEqual(0);
2626
});
2727

28-
xit('computes the sum of an array of one number', function() {
28+
it("computes the sum of an array of one number", function () {
2929
expect(calculator.sum([7])).toEqual(7);
3030
});
3131

32-
xit('computes the sum of an array of two numbers', function() {
33-
expect(calculator.sum([7,11])).toEqual(18);
32+
it("computes the sum of an array of two numbers", function () {
33+
expect(calculator.sum([7, 11])).toEqual(18);
3434
});
3535

36-
xit('computes the sum of an array of many numbers', function() {
37-
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
36+
it("computes the sum of an array of many numbers", function () {
37+
expect(calculator.sum([1, 3, 5, 7, 9])).toEqual(25);
3838
});
3939
});
4040

41-
describe('multiply', function() {
42-
xit('multiplies two numbers', function() {
43-
expect(calculator.multiply([2,4])).toEqual(8);
41+
describe("multiply", function () {
42+
it("multiplies two numbers", function () {
43+
expect(calculator.multiply([2, 4])).toEqual(8);
4444
});
4545

46-
xit('multiplies several numbers', function() {
47-
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
46+
it("multiplies several numbers", function () {
47+
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toEqual(645120);
4848
});
4949
});
5050

51-
describe('power', function() {
52-
xit('raises one number to the power of another number', function() {
53-
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
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
5454
});
5555
});
5656

57-
describe('factorial', function() {
58-
xit('computes the factorial of 0', function() {
57+
describe("factorial", function () {
58+
it("computes the factorial of 0", function () {
5959
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
6060
});
6161

62-
xit('computes the factorial of 1', function() {
62+
it("computes the factorial of 1", function () {
6363
expect(calculator.factorial(1)).toEqual(1);
6464
});
6565

66-
xit('computes the factorial of 2', function() {
66+
it("computes the factorial of 2", function () {
6767
expect(calculator.factorial(2)).toEqual(2);
6868
});
6969

70-
xit('computes the factorial of 5', function() {
70+
it("computes the factorial of 5", function () {
7171
expect(calculator.factorial(5)).toEqual(120);
7272
});
7373

74-
xit('computes the factorial of 10', function() {
74+
it("computes the factorial of 10", function () {
7575
expect(calculator.factorial(10)).toEqual(3628800);
7676
});
7777
});

fibonacci/fibonacci.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fibonacci = function() {
1+
const fibonacci = function (num) {};
22

3-
}
4-
5-
module.exports = fibonacci
3+
module.exports = fibonacci;

palindromes/palindromes.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
const palindromes = function() {
1+
const palindromes = function (word) {
2+
const arr = [];
3+
const lower = word.toLowerCase();
4+
for (let i = 0; i < word.length; i++) {
5+
if (lower.charCodeAt(i) >= 97 && lower.charCodeAt(i) <= 122) {
6+
arr.push(lower[i]);
7+
}
8+
}
9+
const newWord = arr.join("");
10+
const reversed = arr.reverse().join("");
11+
return newWord === reversed;
12+
};
213

3-
}
4-
5-
module.exports = palindromes
14+
module.exports = palindromes;

palindromes/palindromes.spec.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
const palindromes = require('./palindromes')
2-
3-
describe('palindromes', function() {
4-
it('works with single words', function() {
5-
expect(palindromes('racecar')).toEqual(true);
6-
});
7-
xit('works with punctuation ', function() {
8-
expect(palindromes('racecar!')).toEqual(true);
9-
});
10-
xit('works with upper-case letters ', function() {
11-
expect(palindromes('Racecar!')).toEqual(true);
12-
});
13-
xit('works with multiple words', function() {
14-
expect(palindromes('A car, a man, a maraca.')).toEqual(true);
15-
});
16-
xit('works with multiple words', function() {
17-
expect(palindromes('Animal loots foliated detail of stool lamina.')).toEqual(true);
18-
});
19-
xit('doesn\'t just always return true', function() {
20-
expect(palindromes('ZZZZ car, a man, a maraca.')).toEqual(false);
21-
});
1+
const palindromes = require("./palindromes");
222

3+
describe("palindromes", function () {
4+
it("works with single words", function () {
5+
expect(palindromes("racecar")).toEqual(true);
6+
});
7+
it("works with punctuation ", function () {
8+
expect(palindromes("racecar!")).toEqual(true);
9+
});
10+
it("works with upper-case letters ", function () {
11+
expect(palindromes("Racecar!")).toEqual(true);
12+
});
13+
it("works with multiple words", function () {
14+
expect(palindromes("A car, a man, a maraca.")).toEqual(true);
15+
});
16+
it("works with multiple words", function () {
17+
expect(palindromes("Animal loots foliated detail of stool lamina.")).toEqual(true);
18+
});
19+
it("doesn't just always return true", function () {
20+
expect(palindromes("ZZZZ car, a man, a maraca.")).toEqual(false);
21+
});
2322
});

0 commit comments

Comments
 (0)