From 24e55c105c13adb21f0483edde0376c24e2d8ebc Mon Sep 17 00:00:00 2001 From: Piyush Katyal <109459034+piyushk77@users.noreply.github.com> Date: Wed, 11 Oct 2023 02:31:18 +0530 Subject: [PATCH 1/2] feat: add binary to BCD conversion algorithm --- Conversions/BinaryToBcd.js | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Conversions/BinaryToBcd.js diff --git a/Conversions/BinaryToBcd.js b/Conversions/BinaryToBcd.js new file mode 100644 index 0000000000..4ffd618d84 --- /dev/null +++ b/Conversions/BinaryToBcd.js @@ -0,0 +1,68 @@ +/** + * Generates a Binary Coded Decimal (BCD 4-bit representation) equivalent of given binary number. + * @param {string} binaryInput - Binary number in string format to be converted to BCD. + * @returns {string} - A string equivalent of binary coded decimal (4-bit representation). + * + * @see + * For more info: https://en.wikipedia.org/wiki/Binary-coded_decimal + * + * @example + * const binaryInput = '1100010010' + * const bcdNum = binaryToBcd(decimal) + * // bcdNum will be '11110000110' + */ + +const bcdEquivalent = (decimalCharacter) => { + switch (decimalCharacter) { + case '0': + return '0000' + case '1': + return '0001' + case '2': + return '0010' + case '3': + return '0011' + case '4': + return '0100' + case '5': + return '0101' + case '6': + return '0110' + case '7': + return '0111' + case '8': + return '1000' + case '9': + return '1001' + } +} + +const isValidBinary = (binaryInput) => { + for (let i = 0; i < binaryInput.length; i++) { + if (binaryInput[i] !== '0' && binaryInput[i] !== '1') { + return false + } + } + return true +} + +const binaryToBcd = (binaryInput) => { + if (!isValidBinary(binaryInput)) { + throw new Error('Input is not a valid binary number.') + } + + let decimalInput = parseInt(binaryInput, 2) + let decimalString = decimalInput.toString() + let bcdNum = '' + for (let i = 0; i < decimalString.length; i++) { + bcdNum += bcdEquivalent(decimalString[i]) + } + + // Remove unnecessary trailing zeros + let tempDecimal = parseInt(bcdNum, 2) + bcdNum = tempDecimal.toString(2) + + return bcdNum +} + +export { binaryToBcd } From bd0aaa49ee34d4b1a60d5e63a34833e48f82efc9 Mon Sep 17 00:00:00 2001 From: Piyush Katyal <109459034+piyushk77@users.noreply.github.com> Date: Wed, 11 Oct 2023 02:33:41 +0530 Subject: [PATCH 2/2] test: add self-tests for binary to BCD algorithm --- Conversions/test/BinaryToBcd.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Conversions/test/BinaryToBcd.test.js diff --git a/Conversions/test/BinaryToBcd.test.js b/Conversions/test/BinaryToBcd.test.js new file mode 100644 index 0000000000..0b47030e02 --- /dev/null +++ b/Conversions/test/BinaryToBcd.test.js @@ -0,0 +1,25 @@ +import { binaryToBcd } from '../BinaryToBcd' +describe('Binary To BCD', () => { + test.each([ + ['1101101001', '100001110011'], + ['11101111', '1000111001'], + ['1010100010110', '101001110011000'], + ['1111101000', '1000000000000'], + ['111111111111111', '110010011101100111'] + ])( + 'Should return the equivalent BCD of the binary number.', + (binaryNum, expected) => { + expect(binaryToBcd(binaryNum)).toEqual(expected) + } + ) + + test.each([ + ['111100001230', 'Input is not a valid binary number.'], + ['781df', 'Input is not a valid binary number.'], + ['0897111', 'Input is not a valid binary number.'], + ['Dsd1022', 'Input is not a valid binary number.'], + ['00000000000@#$%', 'Input is not a valid binary number.'] + ])('Should return the error message.', (binaryNum, expected) => { + expect(() => binaryToBcd(binaryNum)).toThrowError(expected) + }) +})