From 4aeb3477fffb3660f3d0968b2179b0c41c46a0c3 Mon Sep 17 00:00:00 2001 From: kevin9327 <5299031+kevin9327@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:50:49 +0900 Subject: [PATCH] fix: reject non-binary input in BinaryToHex Invalid digits made hexLookup return undefined, and reversing the digits turned that into the string "denifednu". --- Conversions/BinaryToHex.js | 8 ++++++++ Conversions/test/BinaryToHex.test.js | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Conversions/BinaryToHex.js b/Conversions/BinaryToHex.js index d7f253b222..2a4b944352 100644 --- a/Conversions/BinaryToHex.js +++ b/Conversions/BinaryToHex.js @@ -44,6 +44,14 @@ const hexLookup = (bin) => { } } const binaryToHex = (binaryString) => { + if (typeof binaryString !== 'string') { + throw new TypeError('Argument is not a string type') + } + + if (!/^[01]+$/.test(binaryString)) { + throw new Error('Argument is not a valid binary string!') + } + /* Function for converting Binary to Hex diff --git a/Conversions/test/BinaryToHex.test.js b/Conversions/test/BinaryToHex.test.js index 0444e08e67..20909e35ae 100644 --- a/Conversions/test/BinaryToHex.test.js +++ b/Conversions/test/BinaryToHex.test.js @@ -18,4 +18,16 @@ describe('BinaryToHex', () => { parseInt('1111', 2).toString(16).toUpperCase() ) }) + + it('expects to throw for a non-string argument', () => { + expect(() => binaryToHex(1000)).toThrowError() + expect(() => binaryToHex(null)).toThrowError() + }) + + it('expects to throw for a non-binary string', () => { + expect(() => binaryToHex('2')).toThrowError() + expect(() => binaryToHex('1002')).toThrowError() + expect(() => binaryToHex('1010 1101')).toThrowError() + expect(() => binaryToHex('')).toThrowError() + }) })