Skip to content

Commit 25cd374

Browse files
committed
rot13
1 parent 253b025 commit 25cd374

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

next_bigger_number_with_the_same_digits/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
function nextBigger(n) {
2-
const arrayWithNumbers = String(n).split('');
3-
let index = arrayWithNumbers.length - 1;
4-
5-
index = arrayWithNumbers.findLastIndex((_, index) => arrayWithNumbers[index] > arrayWithNumbers[index - 1])
2+
const arrayWithNumbers = [...String(n)];
3+
let index = arrayWithNumbers.findLastIndex((_, index) => arrayWithNumbers[index] > arrayWithNumbers[index - 1])
64
if (index <= 0) return -1;
75

86
const [suffle, temp] = [arrayWithNumbers.splice(index).sort(), arrayWithNumbers[arrayWithNumbers.length - 1]];
97

10-
index = suffle.findIndex(s => s > temp);
8+
index = suffle.findIndex(value => value > temp);
119
arrayWithNumbers[arrayWithNumbers.length - 1] = suffle[index];
1210
suffle[index] = temp;
1311
return +arrayWithNumbers.concat(suffle).join('');

rot13/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function rot13(str) {
2+
return [...str].reduce((acc, cur) => {
3+
const charCode = cur.toUpperCase().charCodeAt(0) - 65;
4+
return ((0 <= charCode && charCode <= 25) ? acc += String.fromCharCode(cur.charCodeAt(0) + (charCode >= 13 ? -13 : 13)) : acc += cur, acc);
5+
6+
/// Readable
7+
let operator = 0;
8+
if(0 <= charCode && charCode <= 25) operator = charCode >= 13 ? -13 : 13;
9+
acc += String.fromCharCode(cur.charCodeAt(0) + operator);
10+
return acc;
11+
}, '');
12+
}
13+
14+
module.exports = rot13;

rot13/main.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const rot13 = require('./main');
2+
3+
describe('13 rotation tests with one letter', () => {
4+
test('a returns n', () => {
5+
expect(rot13('a')).toBe('n');
6+
});
7+
8+
test('b returns o', () => {
9+
expect(rot13('b')).toBe('o');
10+
});
11+
12+
test('c returns p', () => {
13+
expect(rot13('c')).toBe('p');
14+
});
15+
});
16+
17+
describe('13 rotation tests with more than one letter', () => {
18+
test('ab returns no', () => {
19+
expect(rot13('ab')).toBe('no');
20+
});
21+
22+
test('fl returns sy', () => {
23+
expect(rot13('fl')).toBe('sy');
24+
});
25+
26+
test('hello returns uryyb', () => {
27+
expect(rot13('hello')).toBe('uryyb');
28+
});
29+
});
30+
31+
describe('13 rotation tests with capital letter', () => {
32+
test('A returns N', () => {
33+
expect(rot13('A')).toBe('N');
34+
});
35+
36+
test('AN returns NA', () => {
37+
expect(rot13('AN')).toBe('NA');
38+
});
39+
});
40+
41+
describe('13 rotation ignores spaces and punctuation, numbers, etc', () => {
42+
test('A C returns N P', () => {
43+
expect(rot13('A C')).toBe('N P');
44+
});
45+
46+
test('..." returns EBG13 ...', () => {
47+
expect(rot13('...')).toBe('...');
48+
});
49+
50+
test('@# $ %." returns @# $ %.', () => {
51+
expect(rot13('@# $ %.')).toBe('@# $ %.');
52+
});
53+
});
54+
55+
describe('13 rotation tests with all combinations', () => {
56+
test('ROT13 example. returns EBG13 rknzcyr.', () => {
57+
expect(rot13('ROT13 example.')).toBe('EBG13 rknzcyr.');
58+
});
59+
60+
test('This is my first ROT13 excercise! returns Guvf vf zl svefg EBG13 rkprepvfr!', () => {
61+
expect(rot13('This is my first ROT13 excercise!')).toBe('Guvf vf zl svefg EBG13 rkprepvfr!');
62+
});
63+
});

0 commit comments

Comments
 (0)