From fb9154da400a52e96a486c75ba9d88c57047d8a2 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 17:10:39 +1000 Subject: [PATCH 01/18] [feat] New algorithm --- Maths/ParityOutlier.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Maths/ParityOutlier.js diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js new file mode 100644 index 0000000000..dd6bd2073f --- /dev/null +++ b/Maths/ParityOutlier.js @@ -0,0 +1,29 @@ +/** + * @author mrmagic2020 + * @description The function will find the parity outlier from an array of integers. + * @see https://en.wikipedia.org/wiki/Parity_(mathematics) + * @param {number[]} integers - An array of integers. + * @returns {number} - The parity outlier. + * @example parityOutlier([1, 3, 5, 8, 9]) = 8 + */ +const parityOutlier = (integers) => { + // define new containers + let odds = []; //store odd number(s) + let evens = []; // store even number(s) + + for (let i = 0; i < integers.length; i++) { + if (!Number.isInteger(integers[i])) { // detect non-integer elements + return null; + } else if (integers[i] % 2 === 0) { // an even number + evens.push(integers[i]); + } else { // an odd number + odds.push(integers[i]); + } + } + + if (odds.length === 0 || evens.length === 0) return null; + + return odds.length === 1 ? odds[0] : evens[0]; +} + +export { parityOutlier }; \ No newline at end of file From 791df98fad8eb731af6958b4b79f722b797fb0e8 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 17:13:59 +1000 Subject: [PATCH 02/18] [test] Add new test for ParityOutlier.js --- Maths/test/ParityOutlier.test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Maths/test/ParityOutlier.test.js diff --git a/Maths/test/ParityOutlier.test.js b/Maths/test/ParityOutlier.test.js new file mode 100644 index 0000000000..f5a13f3d28 --- /dev/null +++ b/Maths/test/ParityOutlier.test.js @@ -0,0 +1,30 @@ +import { parityOutlier } from "../ParityOutlier"; + +describe('Testing parityOutlier function', () => { + + it('should return the odd number in an array of even numbers', () => { + expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1); + }); + + it('should return the even number in an array of odd numbers', () => { + expect(parityOutlier([177, 5, 76, 1919])).toBe(76); + }); + + it('should, if the given array has only one integer element, return the integer itself', () => { + expect(parityOutlier([83])).toBe(83); + expect(parityOutlier([54])).toBe(54); + }); + + it('should, if the given array has only an odd and an even number, return the odd outlier', () => { + expect(parityOutlier([1, 2])).toBe(1); + expect(parityOutlier([4, 3])).toBe(3); + }); + + it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { + expect(parityOutlier([])).toBe(null); + expect(parityOutlier([2])).toBe(null); + expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null); + expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null); + expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null); + }); +}); \ No newline at end of file From c7eeff2e59cd3606f58826b6898f49cb16f99654 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 17:24:07 +1000 Subject: [PATCH 03/18] [fix] Reset indentation --- Maths/ParityOutlier.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js index dd6bd2073f..ee86772030 100644 --- a/Maths/ParityOutlier.js +++ b/Maths/ParityOutlier.js @@ -7,23 +7,23 @@ * @example parityOutlier([1, 3, 5, 8, 9]) = 8 */ const parityOutlier = (integers) => { - // define new containers - let odds = []; //store odd number(s) - let evens = []; // store even number(s) + // define new containers + let odds = []; //store odd number(s) + let evens = []; // store even number(s) - for (let i = 0; i < integers.length; i++) { - if (!Number.isInteger(integers[i])) { // detect non-integer elements - return null; - } else if (integers[i] % 2 === 0) { // an even number - evens.push(integers[i]); - } else { // an odd number - odds.push(integers[i]); - } + for (let i = 0; i < integers.length; i++) { + if (!Number.isInteger(integers[i])) { // detect non-integer elements + return null; + } else if (integers[i] % 2 === 0) { // an even number + evens.push(integers[i]); + } else { // an odd number + odds.push(integers[i]); } + } - if (odds.length === 0 || evens.length === 0) return null; + if (odds.length === 0 || evens.length === 0) return null; - return odds.length === 1 ? odds[0] : evens[0]; + return odds.length === 1 ? odds[0] : evens[0]; } export { parityOutlier }; \ No newline at end of file From 2d342dc54b3c6ea89d0ab424dc6c509118b81d1a Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 17:24:29 +1000 Subject: [PATCH 04/18] [fix] Reset indentation --- Maths/test/ParityOutlier.test.js | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Maths/test/ParityOutlier.test.js b/Maths/test/ParityOutlier.test.js index f5a13f3d28..9d8f5de02b 100644 --- a/Maths/test/ParityOutlier.test.js +++ b/Maths/test/ParityOutlier.test.js @@ -2,29 +2,29 @@ import { parityOutlier } from "../ParityOutlier"; describe('Testing parityOutlier function', () => { - it('should return the odd number in an array of even numbers', () => { - expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1); - }); + it('should return the odd number in an array of even numbers', () => { + expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1); + }); - it('should return the even number in an array of odd numbers', () => { - expect(parityOutlier([177, 5, 76, 1919])).toBe(76); - }); + it('should return the even number in an array of odd numbers', () => { + expect(parityOutlier([177, 5, 76, 1919])).toBe(76); + }); - it('should, if the given array has only one integer element, return the integer itself', () => { - expect(parityOutlier([83])).toBe(83); - expect(parityOutlier([54])).toBe(54); - }); + it('should, if the given array has only one integer element, return the integer itself', () => { + expect(parityOutlier([83])).toBe(83); + expect(parityOutlier([54])).toBe(54); + }); - it('should, if the given array has only an odd and an even number, return the odd outlier', () => { - expect(parityOutlier([1, 2])).toBe(1); - expect(parityOutlier([4, 3])).toBe(3); - }); + it('should, if the given array has only an odd and an even number, return the odd outlier', () => { + expect(parityOutlier([1, 2])).toBe(1); + expect(parityOutlier([4, 3])).toBe(3); + }); - it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { - expect(parityOutlier([])).toBe(null); - expect(parityOutlier([2])).toBe(null); - expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null); - expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null); - expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null); - }); + it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { + expect(parityOutlier([])).toBe(null); + expect(parityOutlier([2])).toBe(null); + expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null); + expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null); + expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null); + }); }); \ No newline at end of file From 568763d9fe288d4fdde9b8b3412f22b334fb0dbb Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 17:33:20 +1000 Subject: [PATCH 05/18] [fix] Style changes --- Maths/ParityOutlier.js | 16 +++++++------- Maths/test/ParityOutlier.test.js | 37 ++++++++++++++++---------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js index ee86772030..1aee9b05b2 100644 --- a/Maths/ParityOutlier.js +++ b/Maths/ParityOutlier.js @@ -8,22 +8,22 @@ */ const parityOutlier = (integers) => { // define new containers - let odds = []; //store odd number(s) - let evens = []; // store even number(s) + const odds = [] // store odd number(s) + const evens = [] // store even number(s) for (let i = 0; i < integers.length; i++) { if (!Number.isInteger(integers[i])) { // detect non-integer elements - return null; + return null } else if (integers[i] % 2 === 0) { // an even number - evens.push(integers[i]); + evens.push(integers[i]) } else { // an odd number - odds.push(integers[i]); + odds.push(integers[i]) } } - if (odds.length === 0 || evens.length === 0) return null; + if (odds.length === 0 || evens.length === 0) return null - return odds.length === 1 ? odds[0] : evens[0]; + return odds.length === 1 ? odds[0] : evens[0] } -export { parityOutlier }; \ No newline at end of file +export { parityOutlier } diff --git a/Maths/test/ParityOutlier.test.js b/Maths/test/ParityOutlier.test.js index 9d8f5de02b..42bb5389bc 100644 --- a/Maths/test/ParityOutlier.test.js +++ b/Maths/test/ParityOutlier.test.js @@ -1,30 +1,29 @@ -import { parityOutlier } from "../ParityOutlier"; +import { parityOutlier } from '../ParityOutlier' describe('Testing parityOutlier function', () => { - it('should return the odd number in an array of even numbers', () => { - expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1); - }); + expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1) + }) it('should return the even number in an array of odd numbers', () => { - expect(parityOutlier([177, 5, 76, 1919])).toBe(76); - }); + expect(parityOutlier([177, 5, 76, 1919])).toBe(76) + }) it('should, if the given array has only one integer element, return the integer itself', () => { - expect(parityOutlier([83])).toBe(83); - expect(parityOutlier([54])).toBe(54); - }); + expect(parityOutlier([83])).toBe(83) + expect(parityOutlier([54])).toBe(54) + }) it('should, if the given array has only an odd and an even number, return the odd outlier', () => { - expect(parityOutlier([1, 2])).toBe(1); - expect(parityOutlier([4, 3])).toBe(3); - }); + expect(parityOutlier([1, 2])).toBe(1) + expect(parityOutlier([4, 3])).toBe(3) + }) it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { - expect(parityOutlier([])).toBe(null); - expect(parityOutlier([2])).toBe(null); - expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null); - expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null); - expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null); - }); -}); \ No newline at end of file + expect(parityOutlier([])).toBe(null) + expect(parityOutlier([2])).toBe(null) + expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null) + expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null) + expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null) + }) +}) From 6df2a72ffd43e23acde44ab254e80707daf95df5 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 20:16:35 +1000 Subject: [PATCH 06/18] fix: improve code efficiency and a glitch --- Maths/ParityOutlier.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js index 1aee9b05b2..978cb12fb5 100644 --- a/Maths/ParityOutlier.js +++ b/Maths/ParityOutlier.js @@ -8,22 +8,26 @@ */ const parityOutlier = (integers) => { // define new containers - const odds = [] // store odd number(s) - const evens = [] // store even number(s) + let odds_count = 0 // define counter for odd number(s) + let evens_count = 0 // define counter for even number(s) + let odd, even - for (let i = 0; i < integers.length; i++) { - if (!Number.isInteger(integers[i])) { // detect non-integer elements + for (let e of integers) { + if (!Number.isInteger(e)) { // detect non-integer elements return null - } else if (integers[i] % 2 === 0) { // an even number - evens.push(integers[i]) + } else if (e % 2 === 0) { // an even number + even = e + evens_count++ } else { // an odd number - odds.push(integers[i]) + odd = e; + odds_count++ } } - if (odds.length === 0 || evens.length === 0) return null + if (odds_count === 0 || evens_count === 0) return null // array has only odd/even number(s) + else if (odds_count > 1 && evens_count > 1) return null // array has more than one even and odd number - return odds.length === 1 ? odds[0] : evens[0] + return odds_count === 1 ? odd : even } export { parityOutlier } From 8202eb19c474dc4230668840f344fb36552a3175 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 20:18:10 +1000 Subject: [PATCH 07/18] test: adds a new possible test case --- Maths/test/ParityOutlier.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Maths/test/ParityOutlier.test.js b/Maths/test/ParityOutlier.test.js index 42bb5389bc..ed3307dcd0 100644 --- a/Maths/test/ParityOutlier.test.js +++ b/Maths/test/ParityOutlier.test.js @@ -25,5 +25,6 @@ describe('Testing parityOutlier function', () => { expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null) expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null) expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null) + expect(parityOutlier([1, 3, 5, 7, 2, 4, 6, 8])).toBe(null) }) }) From 2dfc2c07c73af6cc7bb2c92bcf32c54284785113 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Thu, 27 Apr 2023 20:21:37 +1000 Subject: [PATCH 08/18] fix: style fix --- Maths/ParityOutlier.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js index 978cb12fb5..7d7f57bfdc 100644 --- a/Maths/ParityOutlier.js +++ b/Maths/ParityOutlier.js @@ -8,26 +8,26 @@ */ const parityOutlier = (integers) => { // define new containers - let odds_count = 0 // define counter for odd number(s) - let evens_count = 0 // define counter for even number(s) + let oddsCount = 0 // define counter for odd number(s) + let evensCount = 0 // define counter for even number(s) let odd, even - for (let e of integers) { + for (const e of integers) { if (!Number.isInteger(e)) { // detect non-integer elements return null } else if (e % 2 === 0) { // an even number even = e - evens_count++ + evensCount++ } else { // an odd number - odd = e; - odds_count++ + odd = e + oddsCount++ } } - if (odds_count === 0 || evens_count === 0) return null // array has only odd/even number(s) - else if (odds_count > 1 && evens_count > 1) return null // array has more than one even and odd number + if (oddsCount === 0 || evensCount === 0) return null // array has only odd/even number(s) + else if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number - return odds_count === 1 ? odd : even + return oddsCount === 1 ? odd : even } export { parityOutlier } From fd4bda42f96702901aa78d86bbdb057926006897 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 09:42:30 +1000 Subject: [PATCH 09/18] fix: delete redundant comments and else statements --- Maths/ParityOutlier.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js index 7d7f57bfdc..fb838db128 100644 --- a/Maths/ParityOutlier.js +++ b/Maths/ParityOutlier.js @@ -7,7 +7,6 @@ * @example parityOutlier([1, 3, 5, 8, 9]) = 8 */ const parityOutlier = (integers) => { - // define new containers let oddsCount = 0 // define counter for odd number(s) let evensCount = 0 // define counter for even number(s) let odd, even @@ -15,7 +14,9 @@ const parityOutlier = (integers) => { for (const e of integers) { if (!Number.isInteger(e)) { // detect non-integer elements return null - } else if (e % 2 === 0) { // an even number + } + + if (e % 2 === 0) { // an even number even = e evensCount++ } else { // an odd number @@ -25,7 +26,7 @@ const parityOutlier = (integers) => { } if (oddsCount === 0 || evensCount === 0) return null // array has only odd/even number(s) - else if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number + if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number return oddsCount === 1 ? odd : even } From 4a4d7e3b505dace44a7c69e88a547013d479bbc6 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 09:45:27 +1000 Subject: [PATCH 10/18] [fix] style fix --- Maths/ParityOutlier.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js index fb838db128..7642541ffb 100644 --- a/Maths/ParityOutlier.js +++ b/Maths/ParityOutlier.js @@ -15,7 +15,6 @@ const parityOutlier = (integers) => { if (!Number.isInteger(e)) { // detect non-integer elements return null } - if (e % 2 === 0) { // an even number even = e evensCount++ From 2c0e619e4248894d3e23c0bcd3987b59cbe257d5 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:35:08 +1000 Subject: [PATCH 11/18] feat: New algorithm --- Ciphers/MorseCode.js | 85 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Ciphers/MorseCode.js diff --git a/Ciphers/MorseCode.js b/Ciphers/MorseCode.js new file mode 100644 index 0000000000..adb37901ee --- /dev/null +++ b/Ciphers/MorseCode.js @@ -0,0 +1,85 @@ +/** + * @author mrmagic2020 + * @description Enciphers a combination of letters, numbers and symbols into morse code. + * @see https://en.wikipedia.org/wiki/Morse_code + * @param {string} msg The message to be enciphered. + * @param {string} dot Symbol representing the dots. + * @param {string} dash Symbol representing the dash. + * @returns {string} Enciphered morse code. + * @example morse('Hello World!') = '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--' + */ +const morse = (msg, dot = `*`, dash = `-`) => { + const key = { + A: '*-', + B: '-***', + C: '-*-*', + D: '-**', + E: '*', + F: '**-*', + G: '--*', + H: '****', + I: '**', + J: '*---', + K: '-*-', + L: '*-**', + M: '--', + N: '-*', + O: '---', + P: '*--*', + Q: '--*-', + R: '*-*', + S: '***', + T: '-', + U: '**-', + V: '***-', + W: '*--', + X: '-**-', + Y: '-*--', + Z: '--**', + '1': '*----', + '2': '**---', + '3': '***--', + '4': '****-', + '5': '*****', + '6': '-****', + '7': '--***', + '8': '---**', + '9': '----*', + '0': '-----', + '.': '*-*-*-', + ',': '--**--', + '?': '**--**', + '!': '-*-*--', + '\'': '*----*', + '"': '*-**-*', + '(': '-*--*', + ')': '-*--*-', + '&': '*-***', + ':': '---***', + ';': '-*-*-*', + '/': '-**-*', + '_': '**--*-', + '=': '-***-', + '+': '*-*-*', + '-': '-****-', + '$': '***-**-', + '@': '*--*-*' + } + + let newMsg = '' + + msg.toString().split(``).forEach((e) => { + if (/[a-zA-Z]/.test(e)) { + newMsg += key[e.toUpperCase()] + } else if (Object.keys(key).includes(e)) { + newMsg += key[e] + } else { + newMsg += e + } + newMsg += ' ' + }) + + return newMsg.trim() +} + +export { morse } From e318e11d78088588a84cb7a066db6616b529345b Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:46:02 +1000 Subject: [PATCH 12/18] fix: fixed custom code symbols --- Ciphers/MorseCode.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Ciphers/MorseCode.js b/Ciphers/MorseCode.js index adb37901ee..a27cca7933 100644 --- a/Ciphers/MorseCode.js +++ b/Ciphers/MorseCode.js @@ -8,7 +8,7 @@ * @returns {string} Enciphered morse code. * @example morse('Hello World!') = '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--' */ -const morse = (msg, dot = `*`, dash = `-`) => { +const morse = (msg, dot = '*', dash = '-') => { const key = { A: '*-', B: '-***', @@ -68,11 +68,11 @@ const morse = (msg, dot = `*`, dash = `-`) => { let newMsg = '' - msg.toString().split(``).forEach((e) => { + msg.toString().split('').forEach((e) => { if (/[a-zA-Z]/.test(e)) { - newMsg += key[e.toUpperCase()] + newMsg += key[e.toUpperCase()].replaceAll('*', dot).replaceAll('-', dash) } else if (Object.keys(key).includes(e)) { - newMsg += key[e] + newMsg += key[e].replaceAll('*', dot).replaceAll('-', dash) } else { newMsg += e } From 5435e8d58347edaaaab395df0b0f432c496d83df Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:47:04 +1000 Subject: [PATCH 13/18] test: add test for MorseCode --- Ciphers/test/MorseCode.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Ciphers/test/MorseCode.test.js diff --git a/Ciphers/test/MorseCode.test.js b/Ciphers/test/MorseCode.test.js new file mode 100644 index 0000000000..a73ac4ab1c --- /dev/null +++ b/Ciphers/test/MorseCode.test.js @@ -0,0 +1,12 @@ +import { morse } from '../MorseCode' + +describe('Testing morse function', () => { + it('should return an enciphered string with a given input string', () => { + expect(morse('Hello World!')).toBe('**** * *-** *-** --- *-- --- *-* *-** -** -*-*--') + expect(morse('1+1=2')).toBe(`*---- *-*-* *---- -***- **---`) + }); + + it('should leave symbols that does not have its corresponding morse representation', () => { + expect(morse('© 2023 GitHub, Inc.')).toBe('© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-'); + }) +}) From 09e6fdd79624d9a99b5e1ea163a8313221fbfa58 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:48:58 +1000 Subject: [PATCH 14/18] test: add case with custom code symbols --- Ciphers/test/MorseCode.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Ciphers/test/MorseCode.test.js b/Ciphers/test/MorseCode.test.js index a73ac4ab1c..2768abf9d1 100644 --- a/Ciphers/test/MorseCode.test.js +++ b/Ciphers/test/MorseCode.test.js @@ -9,4 +9,8 @@ describe('Testing morse function', () => { it('should leave symbols that does not have its corresponding morse representation', () => { expect(morse('© 2023 GitHub, Inc.')).toBe('© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-'); }) + + it('should be able to accept custom morse code symbols', () => { + expect(morse('Nodejs', '.', '|')).toBe('|. ||| |.. . .||| ...') + }) }) From 67591138d64de7a6314531275a980a55fb8b1dc6 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:51:39 +1000 Subject: [PATCH 15/18] delete files from main branch --- Maths/ParityOutlier.js | 33 -------------------------------- Maths/test/ParityOutlier.test.js | 30 ----------------------------- 2 files changed, 63 deletions(-) delete mode 100644 Maths/ParityOutlier.js delete mode 100644 Maths/test/ParityOutlier.test.js diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js deleted file mode 100644 index 7642541ffb..0000000000 --- a/Maths/ParityOutlier.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * @author mrmagic2020 - * @description The function will find the parity outlier from an array of integers. - * @see https://en.wikipedia.org/wiki/Parity_(mathematics) - * @param {number[]} integers - An array of integers. - * @returns {number} - The parity outlier. - * @example parityOutlier([1, 3, 5, 8, 9]) = 8 - */ -const parityOutlier = (integers) => { - let oddsCount = 0 // define counter for odd number(s) - let evensCount = 0 // define counter for even number(s) - let odd, even - - for (const e of integers) { - if (!Number.isInteger(e)) { // detect non-integer elements - return null - } - if (e % 2 === 0) { // an even number - even = e - evensCount++ - } else { // an odd number - odd = e - oddsCount++ - } - } - - if (oddsCount === 0 || evensCount === 0) return null // array has only odd/even number(s) - if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number - - return oddsCount === 1 ? odd : even -} - -export { parityOutlier } diff --git a/Maths/test/ParityOutlier.test.js b/Maths/test/ParityOutlier.test.js deleted file mode 100644 index ed3307dcd0..0000000000 --- a/Maths/test/ParityOutlier.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import { parityOutlier } from '../ParityOutlier' - -describe('Testing parityOutlier function', () => { - it('should return the odd number in an array of even numbers', () => { - expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1) - }) - - it('should return the even number in an array of odd numbers', () => { - expect(parityOutlier([177, 5, 76, 1919])).toBe(76) - }) - - it('should, if the given array has only one integer element, return the integer itself', () => { - expect(parityOutlier([83])).toBe(83) - expect(parityOutlier([54])).toBe(54) - }) - - it('should, if the given array has only an odd and an even number, return the odd outlier', () => { - expect(parityOutlier([1, 2])).toBe(1) - expect(parityOutlier([4, 3])).toBe(3) - }) - - it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { - expect(parityOutlier([])).toBe(null) - expect(parityOutlier([2])).toBe(null) - expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null) - expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null) - expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null) - expect(parityOutlier([1, 3, 5, 7, 2, 4, 6, 8])).toBe(null) - }) -}) From 8288b79860f8895792fc00110be252077c2ef82f Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:57:02 +1000 Subject: [PATCH 16/18] fix: style fix --- Ciphers/test/MorseCode.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Ciphers/test/MorseCode.test.js b/Ciphers/test/MorseCode.test.js index 2768abf9d1..5dd4e07959 100644 --- a/Ciphers/test/MorseCode.test.js +++ b/Ciphers/test/MorseCode.test.js @@ -3,11 +3,11 @@ import { morse } from '../MorseCode' describe('Testing morse function', () => { it('should return an enciphered string with a given input string', () => { expect(morse('Hello World!')).toBe('**** * *-** *-** --- *-- --- *-* *-** -** -*-*--') - expect(morse('1+1=2')).toBe(`*---- *-*-* *---- -***- **---`) - }); + expect(morse('1+1=2')).toBe('*---- *-*-* *---- -***- **---') + }) it('should leave symbols that does not have its corresponding morse representation', () => { - expect(morse('© 2023 GitHub, Inc.')).toBe('© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-'); + expect(morse('© 2023 GitHub, Inc.')).toBe('© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-') }) it('should be able to accept custom morse code symbols', () => { From f16b07b84fc535ff080e9eff7aad9566ba503f91 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 13:58:59 +1000 Subject: [PATCH 17/18] fix: style fix --- Ciphers/MorseCode.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ciphers/MorseCode.js b/Ciphers/MorseCode.js index a27cca7933..0c78cbe5d2 100644 --- a/Ciphers/MorseCode.js +++ b/Ciphers/MorseCode.js @@ -2,10 +2,10 @@ * @author mrmagic2020 * @description Enciphers a combination of letters, numbers and symbols into morse code. * @see https://en.wikipedia.org/wiki/Morse_code - * @param {string} msg The message to be enciphered. + * @param {string} msg The message to be enciphered. * @param {string} dot Symbol representing the dots. * @param {string} dash Symbol representing the dash. - * @returns {string} Enciphered morse code. + * @returns {string} Enciphered morse code. * @example morse('Hello World!') = '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--' */ const morse = (msg, dot = '*', dash = '-') => { From 6d2bde95a118a551b027c79ea94f74f81dfe3551 Mon Sep 17 00:00:00 2001 From: mrmagic2020 <2732298611@qq.com> Date: Fri, 28 Apr 2023 19:49:07 +1000 Subject: [PATCH 18/18] fix: delete unnecessary quotes --- Ciphers/MorseCode.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Ciphers/MorseCode.js b/Ciphers/MorseCode.js index 0c78cbe5d2..9c64a39a25 100644 --- a/Ciphers/MorseCode.js +++ b/Ciphers/MorseCode.js @@ -36,16 +36,16 @@ const morse = (msg, dot = '*', dash = '-') => { X: '-**-', Y: '-*--', Z: '--**', - '1': '*----', - '2': '**---', - '3': '***--', - '4': '****-', - '5': '*****', - '6': '-****', - '7': '--***', - '8': '---**', - '9': '----*', - '0': '-----', + 1: '*----', + 2: '**---', + 3: '***--', + 4: '****-', + 5: '*****', + 6: '-****', + 7: '--***', + 8: '---**', + 9: '----*', + 0: '-----', '.': '*-*-*-', ',': '--**--', '?': '**--**', @@ -58,11 +58,11 @@ const morse = (msg, dot = '*', dash = '-') => { ':': '---***', ';': '-*-*-*', '/': '-**-*', - '_': '**--*-', + _: '**--*-', '=': '-***-', '+': '*-*-*', '-': '-****-', - '$': '***-**-', + $: '***-**-', '@': '*--*-*' } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy