Skip to content

Commit c9f5a31

Browse files
committed
Moved all files to Typescript
1 parent 86f9358 commit c9f5a31

File tree

28 files changed

+197
-190
lines changed

28 files changed

+197
-190
lines changed

src/binary-search/index.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import binarySearch from ".";
22

3-
describe('Binary search: ', () => {
4-
test('should return true (array contains key)', () => {
3+
describe("Binary search: ", () => {
4+
test("should return true (array contains key)", () => {
55
const numArray = [5, 7, 12, 16, 36, 56, 71];
66
const key = 56;
77

88
expect(binarySearch(numArray, key)).toBeTruthy();
99
});
1010

11-
test('should return false (array does not contains key)', () => {
11+
test("should return false (array does not contains key)", () => {
1212
const numArray = [5, 7, 12, 16, 36, 56, 71];
1313
const key = 99;
1414

1515
expect(binarySearch(numArray, key)).toBeFalsy();
1616
});
1717

18-
test('should return false (empty array passed)', () => {
18+
test("should return false (empty array passed)", () => {
1919
const key = -33;
2020

2121
expect(binarySearch([], key)).toBeFalsy();

src/binary-search/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ function binarySearch(numArray: number[] = [], key: number): boolean {
1212
let leftHalf = numArray.slice(0, middleIndex);
1313

1414
return binarySearch(leftHalf, key);
15-
}
16-
else if (key > middleElem && numArrLength > 1) {
15+
} else if (key > middleElem && numArrLength > 1) {
1716
let rightHalf = numArray.slice(middleIndex);
1817

1918
return binarySearch(rightHalf, key);
20-
}
21-
else {
19+
} else {
2220
return key === middleElem;
2321
}
2422
}

src/bubble-sort/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import bubbleSort from "./";
22

3-
describe('Bubble sort: ', () => {
4-
test('should return sorted array (only positive numbers)', () => {
3+
describe("Bubble sort: ", () => {
4+
test("should return sorted array (only positive numbers)", () => {
55
const rawArray = [5, 3, 8, 2, 1, 4];
66
const sortedArray = [1, 2, 3, 4, 5, 8];
77

88
expect(bubbleSort(rawArray)).toEqual(sortedArray);
99
});
1010

11-
test('should return sorted array (positive and negative numbers)', () => {
11+
test("should return sorted array (positive and negative numbers)", () => {
1212
const rawArray = [-7, 13, 4, 1e8, 0, -15];
1313
const sortedArray = [-15, -7, 0, 4, 13, 1e8];
1414

src/caesar-cipher/index.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import caesarCipher from "./";
22

3-
describe('Caesar cipher:', () => {
4-
test('should return transformed string (number 3 given)', () => {
5-
const initialString = 'Zoo Keeper';
6-
const expectedString = 'Crr Nhhshu';
3+
describe("Caesar cipher:", () => {
4+
test("should return transformed string (number 3 given)", () => {
5+
const initialString = "Zoo Keeper";
6+
const expectedString = "Crr Nhhshu";
77

88
const shiftNumber = 3;
99

1010
expect(caesarCipher(initialString, shiftNumber)).toEqual(expectedString);
1111
});
1212

13-
test('should return transformed string (number 84 given)', () => {
14-
const initialString = 'I love Javascript'
15-
const expectedString = 'O rubk Pgbgyixovz';
13+
test("should return transformed string (number 84 given)", () => {
14+
const initialString = "I love Javascript";
15+
const expectedString = "O rubk Pgbgyixovz";
1616

1717
const shiftNumber = 84;
1818

1919
expect(caesarCipher(initialString, shiftNumber)).toEqual(expectedString);
2020
});
2121

22-
test('should return empty string (since empty string given)', () => {
22+
test("should return empty string (since empty string given)", () => {
2323
const shiftNumber = 99;
2424

25-
expect(caesarCipher('', shiftNumber)).toEqual('');
25+
expect(caesarCipher("", shiftNumber)).toEqual("");
2626
});
2727
});

src/caesar-cipher/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
function caesarCipher(str: string, num: number): string {
2-
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
2+
const alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
33
const lowerStr = str.toLowerCase();
44
const position = num % alphabet.length;
55

6-
let newString = '';
6+
let newString = "";
77

88
for (let i = 0; i < lowerStr.length; i++) {
99
const currentLetter = lowerStr[i];
1010

11-
if (currentLetter === ' ') {
11+
if (currentLetter === " ") {
1212
newString += currentLetter;
13-
}
14-
else {
13+
} else {
1514
let newIndex = alphabet.indexOf(currentLetter) + position;
1615

1716
if (newIndex > 25) {
@@ -22,9 +21,10 @@ function caesarCipher(str: string, num: number): string {
2221
newIndex = alphabet.length + newIndex;
2322
}
2423

25-
newString += (str[i] === str[i].toUpperCase())
26-
? alphabet[newIndex].toUpperCase()
27-
: alphabet[newIndex];
24+
newString +=
25+
str[i] === str[i].toUpperCase()
26+
? alphabet[newIndex].toUpperCase()
27+
: alphabet[newIndex];
2828
}
2929
}
3030

src/fibonacci/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import fibonacci from "./";
22

3-
describe('Fibonacci:', () => {
4-
test('should return number on position 7', () => {
3+
describe("Fibonacci:", () => {
4+
test("should return number on position 7", () => {
55
expect(fibonacci(7)).toEqual(13);
66
});
77

8-
test('should return number on position 2', () => {
8+
test("should return number on position 2", () => {
99
expect(fibonacci(2)).toEqual(1);
1010
});
1111
});

src/fizz-buzz/index.spec.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
import fizzBuzz from ".";
22

3-
describe('Fizz Buzz: ', () => {
4-
test('tests with 2 as given number', () => {
3+
describe("Fizz Buzz: ", () => {
4+
test("tests with 2 as given number", () => {
55
const expected = [1, 2];
66
const number = 2;
77

88
expect(fizzBuzz(number)).toEqual(expected);
99
});
1010

11-
test('tests with 15 as given number', () => {
12-
const expected = [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"];
11+
test("tests with 15 as given number", () => {
12+
const expected = [
13+
1,
14+
2,
15+
"Fizz",
16+
4,
17+
"Buzz",
18+
"Fizz",
19+
7,
20+
8,
21+
"Fizz",
22+
"Buzz",
23+
11,
24+
"Fizz",
25+
13,
26+
14,
27+
"FizzBuzz",
28+
];
1329
const number = 15;
1430

1531
expect(fizzBuzz(number)).toEqual(expected);

src/fizz-buzz/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ function fizzBuzz(num: number): (string | number)[] {
33

44
for (let i = 1; i <= num; i++) {
55
if (i % 15 === 0) {
6-
result.push('FizzBuzz');
7-
}
8-
else if (i % 3 === 0) {
9-
result.push('Fizz');
10-
}
11-
else if (i % 5 === 0) {
12-
result.push('Buzz');
13-
}
14-
else {
6+
result.push("FizzBuzz");
7+
} else if (i % 3 === 0) {
8+
result.push("Fizz");
9+
} else if (i % 5 === 0) {
10+
result.push("Buzz");
11+
} else {
1512
result.push(i);
1613
}
1714
}
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
import harmlessRansomNote from ".";
22

3-
describe('Harmless ransom note:', () => {
4-
test('should return true for given string and magazine text (similar cases)', () => {
5-
const noteText = 'this all in';
6-
const magazineText = 'this is all the magazine text in the magazine';
3+
describe("Harmless ransom note:", () => {
4+
test("should return true for given string and magazine text (similar cases)", () => {
5+
const noteText = "this all in";
6+
const magazineText = "this is all the magazine text in the magazine";
77

88
expect(harmlessRansomNote(noteText, magazineText)).toBeTruthy();
99
});
1010

11-
test('should return true for given string and magazine text (one word duplicated)', () => {
12-
const noteText = 'you are very very beautiful';
13-
const magazineText = 'Sarah, are you listening for this very beautiful song that I\'ve sent you';
11+
test("should return true for given string and magazine text (one word duplicated)", () => {
12+
const noteText = "you are very very beautiful";
13+
const magazineText =
14+
"Sarah, are you listening for this very beautiful song that I've sent you";
1415

1516
expect(harmlessRansomNote(noteText, magazineText)).toBeFalsy();
1617
});
1718

18-
test('should return false for given string and magazine text (different cases)', () => {
19-
const noteText = 'hello world';
20-
const magazineText = 'Hello from other World';
19+
test("should return false for given string and magazine text (different cases)", () => {
20+
const noteText = "hello world";
21+
const magazineText = "Hello from other World";
2122

2223
expect(harmlessRansomNote(noteText, magazineText)).toBeFalsy();
2324
});
2425

25-
test('should return false for empty string and magazine text', () => {
26-
const magazineText = 'Magazine text';
26+
test("should return false for empty string and magazine text", () => {
27+
const magazineText = "Magazine text";
2728

28-
expect(harmlessRansomNote('', magazineText)).toBeFalsy();
29+
expect(harmlessRansomNote("", magazineText)).toBeFalsy();
2930
});
3031

31-
test('should return false for empty string and empty magazine text', () => {
32-
expect(harmlessRansomNote('', '')).toBeTruthy();
32+
test("should return false for empty string and empty magazine text", () => {
33+
expect(harmlessRansomNote("", "")).toBeTruthy();
3334
});
3435
});

src/is-palindrome/index.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import isPalindrome from ".";
22

3-
describe('Palindrome:', () => {
4-
test('should return true for given string (similar cases)', () => {
5-
const string = 'race car';
3+
describe("Palindrome:", () => {
4+
test("should return true for given string (similar cases)", () => {
5+
const string = "race car";
66

77
expect(isPalindrome(string)).toBeTruthy();
88
});
99

10-
test('should return true for given string (different cases, punctuation marks)', () => {
11-
const string = 'A man, a plan, a canal, Panama!';
10+
test("should return true for given string (different cases, punctuation marks)", () => {
11+
const string = "A man, a plan, a canal, Panama!";
1212

1313
expect(isPalindrome(string)).toBeTruthy();
1414
});
1515

16-
test('should return false for given string', () => {
17-
const string = 'A light flight';
16+
test("should return false for given string", () => {
17+
const string = "A light flight";
1818

1919
expect(isPalindrome(string)).toBeFalsy();
2020
});
2121

22-
test('should return true for empty string', () => {
23-
expect(isPalindrome('')).toBeTruthy();
22+
test("should return true for empty string", () => {
23+
expect(isPalindrome("")).toBeTruthy();
2424
});
2525
});

0 commit comments

Comments
 (0)