From 1e333937a13f5ddfcedd849881dc17130a83ee12 Mon Sep 17 00:00:00 2001 From: Hopeuche360 Date: Wed, 14 Sep 2022 12:33:25 +0100 Subject: [PATCH] submit --- src/binary-reversal/index.js | 22 +++++++++++++++++++++- src/list-sorting/index.js | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/binary-reversal/index.js b/src/binary-reversal/index.js index 965bccf4..cf2fc9ca 100644 --- a/src/binary-reversal/index.js +++ b/src/binary-reversal/index.js @@ -3,6 +3,26 @@ * * * @param {string} value */ -function binaryReversal(value) {} +/*My thought process: +- remove the string by converting the value to integar; +- convert to binary usingthe toString method; +- pad the binary num to 8 bits; +- reverse the binary num; +- convert the binary num to a decimal num, as well convert the num to string; +- then return the decimal num; +*/ + +function binaryReversal(value) { + let toIntegar = parseInt(value); + let toBinary = toIntegar.toString(2); + + let paddedNum = toBinary.padStart(8, 0); + let reversedNum = paddedNum.split("").reverse().join(""); + + let decimalNum = parseInt(reversedNum, 2).toString(); + return decimalNum; +}; + + module.exports = binaryReversal; diff --git a/src/list-sorting/index.js b/src/list-sorting/index.js index 6636c20d..9466bf9f 100644 --- a/src/list-sorting/index.js +++ b/src/list-sorting/index.js @@ -1,3 +1,21 @@ -function listSorting(needle, haystack) {} +/*My thought process: +- remove the string by converting the value to integar; +- convert to binary usingthe toString method; +- pad the binary num to 8 bits; +- reverse the binary num; +- convert the binary num to a decimal num, as well convert the num to string; +- then return the decimal num; +*/ + +function binaryReversal(value) { + let toIntegar = parseInt(value); + let toBinary = toIntegar.toString(2); + + let paddedNum = toBinary.padStart(8, 0); + let reversedNum = paddedNum.split("").reverse().join(""); + + let decimalNum = parseInt(reversedNum, 2).toString(); + return decimalNum; +}; module.exports = listSorting;