parent
afb28bb95d
commit
54c0dcd305
@ -1,74 +1,60 @@
|
|||||||
(function (document, window, domIsReady, undefined) {
|
((document, window, domIsReady, undefined) => {
|
||||||
|
const convertBin2Dec = binaryExpr => {
|
||||||
function convertBin2Dec(binaryExpr) {
|
// Formulae
|
||||||
const bin2Dec = {
|
// input = 1 => output = 1*(2^0) = 1
|
||||||
'0000': 0,
|
// input = 10 => Output = (0*(2^0))+(1*(2^1)) = 2
|
||||||
'0001': 1,
|
// so we reverse and iterate from the back
|
||||||
'0010': 2,
|
const reverseBinaryText = binaryExpr
|
||||||
'0011': 3,
|
.split("")
|
||||||
'0100': 4,
|
.map(Number) // convert to number from string
|
||||||
'0101': 5,
|
.reverse();
|
||||||
'0110': 6,
|
// claculate the result by accumulating previous value
|
||||||
'0111': 7,
|
const result = reverseBinaryText.reduce(
|
||||||
'1000': 8,
|
(accumulator, currentValue, indx) =>
|
||||||
'1001': 9,
|
accumulator + currentValue * Math.pow(2, indx)
|
||||||
'1010': 10,
|
);
|
||||||
'1011': 11,
|
|
||||||
'1100': 12,
|
|
||||||
'1101': 13,
|
|
||||||
'1110': 14,
|
|
||||||
'1111': 15,
|
|
||||||
};
|
|
||||||
const len = binaryExpr.length;
|
|
||||||
binaryExpr = len % 4 === 0 ? binaryExpr : '0'.repeat(4 - len % 4) + binaryExpr;
|
|
||||||
|
|
||||||
const maxPow = binaryExpr.length / 4;
|
|
||||||
let result = 0, pow = 1;
|
|
||||||
for (let i = 0; i < maxPow; i++) {
|
|
||||||
result += bin2Dec[binaryExpr.slice(4 * i, 4 * (i + 1))] * pow;
|
|
||||||
pow *= 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
};
|
||||||
|
|
||||||
function validate() {
|
const validate = () => {
|
||||||
const formEl = document.getElementById('form');
|
const formEl = document.getElementById("form");
|
||||||
const inputEl = formEl.querySelector('input');
|
const inputEl = formEl.querySelector("input");
|
||||||
const errMsgEl = formEl.querySelector('.error-message');
|
const errMsgEl = formEl.querySelector(".error-message");
|
||||||
const outEl = document.getElementById('output');
|
const outEl = document.getElementById("output");
|
||||||
|
|
||||||
let expr = '', isValid = false;
|
let expr = "",
|
||||||
|
isValid = false;
|
||||||
|
|
||||||
const handleValid = () => {
|
const handleValid = () => {
|
||||||
errMsgEl.className = 'error-message hidden';
|
errMsgEl.className = "error-message hidden";
|
||||||
errMsgEl.innerHTML = '';
|
errMsgEl.innerHTML = "";
|
||||||
const decExpr = convertBin2Dec(expr);
|
const decExpr = convertBin2Dec(expr);
|
||||||
outEl.innerHTML = `Decimal equivalent of ${expr} : ${decExpr}`;
|
outEl.innerHTML = `Decimal equivalent of ${expr} : ${decExpr}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleInvalid = () => {
|
const handleInvalid = () => {
|
||||||
errMsgEl.className = 'error-message';
|
errMsgEl.className = "error-message";
|
||||||
if (expr.length === 0) {
|
if (expr.length === 0) {
|
||||||
errMsgEl.innerHTML = 'Please enter a binary expression';
|
errMsgEl.innerHTML = "Please enter a binary expression";
|
||||||
}
|
}
|
||||||
if (/[^0-1]+/g.test(expr)) {
|
if (/[^0-1]+/g.test(expr)) {
|
||||||
errMsgEl.innerHTML = 'Only 0, 1 is allowed!';
|
errMsgEl.innerHTML = "Only 0, 1 is allowed!";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
formEl.addEventListener('submit', e => e.preventDefault());
|
formEl.addEventListener("submit", e => e.preventDefault());
|
||||||
|
|
||||||
inputEl.addEventListener('keyup', (e) => {
|
inputEl.addEventListener("keyup", e => {
|
||||||
outEl.innerHTML = '';
|
outEl.innerHTML = "";
|
||||||
expr = e.target.value;
|
expr = e.target.value;
|
||||||
const validInputRegEx = /^[0-1]+$/g;
|
const validInputRegEx = /^[0-1]+$/g;
|
||||||
isValid = validInputRegEx.test(expr);
|
isValid = validInputRegEx.test(expr);
|
||||||
isValid ? handleValid() : handleInvalid();
|
isValid ? handleValid() : handleInvalid();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
domIsReady(function () {
|
domIsReady(() => {
|
||||||
validate();
|
validate();
|
||||||
});
|
});
|
||||||
})(document, window, domIsReady);
|
})(document, window, domIsReady);
|
||||||
|
Loading…
Reference in new issue