Merge branch 'solutions' into master

This commit is contained in:
xandora
2021-05-09 00:10:19 +12:00
committed by GitHub
2939 changed files with 303672 additions and 173 deletions
+24 -3
View File
@@ -1,5 +1,26 @@
const caesar = function() {
const caesar = function(string, shift) {
return string
.split("")
.map(char => shiftChar(char, shift))
.join("");
};
}
const codeSet = code => (code < 97 ? 65 : 97);
module.exports = caesar
// this function is just a fancy way of doing % so that it works with negative numbers
// see this link for details:
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
const mod = (n, m) => (n % m + m) % m;
const shiftChar = (char, shift) => {
const code = char.charCodeAt();
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
return String.fromCharCode(
mod(code + shift - codeSet(code), 26) + codeSet(code)
);
}
return char;
};
module.exports = caesar;
+14 -14
View File
@@ -1,23 +1,23 @@
const caesar = require('./caesar')
const caesar = require("./caesar");
describe('caesar', function() {
it('works with single letters', function() {
expect(caesar('A', 1)).toEqual('B');
describe("caesar", () => {
it("works with single letters", () => {
expect(caesar("A", 1)).toEqual("B");
});
xit('works with words', function() {
expect(caesar('Aaa', 1)).toEqual('Bbb');
it("works with words", () => {
expect(caesar("Aaa", 1)).toEqual("Bbb");
});
xit('works with phrases', function() {
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
it("works with phrases", () => {
expect(caesar("Hello, World!", 5)).toEqual("Mjqqt, Btwqi!");
});
xit('works with negative shift', function() {
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
it("works with negative shift", () => {
expect(caesar("Mjqqt, Btwqi!", -5)).toEqual("Hello, World!");
});
xit('wraps', function() {
expect(caesar('Z', 1)).toEqual('A');
it("wraps", () => {
expect(caesar("Z", 1)).toEqual("A");
});
xit('works with large shift factors', function() {
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
it("works with large shift factors", () => {
expect(caesar("Hello, World!", 75)).toEqual("Ebiil, Tloia!");
});
xit('works with large negative shift factors', function() {
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');