1const binaryArrayToNumber = arr => {
2 let len = arr.length
3 let pow = []
4 let decimal = []
5 for(let i = 0; i <= len - 1; i++){
6 pow.unshift(i)
7 }
8 arr.forEach((x,index) => {
9 decimal.push(x*2**pow[index])
10 })
11 let toDecimal = decimal.reduce((acc, curr) => acc + curr, 0)
12 return toDecimal
13};
14console.log(binaryArrayToNumber([1, 0, 1, 1]))
1const input = document.querySelector("#input");
2const output = document.querySelector("#output");
3const btn = document.querySelector("#btn");
4const error = document.querySelector("#error-msg");
5
6
7function Bin2Dec() {
8 const regEx = /^[0-1]+$/;
9
10 if(input.value.match(regEx)) {
11 const binArr = input.value.split('').reverse();
12 let decNo = 0;
13
14 binArr.forEach((item, index) => item === '1' ? decNo += Math.pow(2, index) : void 0);
15
16 output.value = decNo.toString();
17 output.style.cursor = 'text';
18 }else {
19 error.style.display = 'block';
20 }
21}
22
23
24btn.addEventListener('click', () => {
25 error.style.display = 'none';
26 Bin2Dec();
27})