I am working on a Binary2Decimal convertor app, where when the user enters other than 0s and 1s in input field, then while clicking on convert it should alert the user to enter valid binary number.
Below is my code:
const binary = document.querySelector('.decimalConvertor__binary--bin');
const decimal = document.querySelector('.decimalConvertor__decimal--dec');
const submit = document.querySelector('.decimalConvertor__submit--sub');
submit.addEventListener('click', e => {
const bin = binary.value;
if(bin !== '' && ' ') {
if(bin.length <=8) {
let dec = 0;
for (let i = bin.length-1; i >=0; i--) {
dec += parseInt(bin[i]) * Math.pow(2, bin.length - 1 - i);
}
decimal.value = dec
}else {
alert('enter less than 8 numbers')
}
}else {
alert('Please enter valid binary number')
}
});
ifstatement) but it should work. What's the problem you have with your solution?