1

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')
  } 
});
2
  • 1
    I noted some problem into the code (for example the condition in if statement) but it should work. What's the problem you have with your solution? Commented May 27, 2020 at 8:36
  • I need to add this code: If user enter number other than 0 & 1 (eg: 101012), and click on convert, alert('not valid binary number') Commented May 27, 2020 at 8:37

2 Answers 2

1

try this, it uses regex to test whether the string contains numbers other than 1 and 0, if it does, then it shows the alert to enter a valid binary number:

var re = new RegExp("([23456789])");

submit.addEventListener('click', e => {
  const bin = binary.value;
  if(bin && !re.test(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')
  } 
});

Here's a website where you can create regexes and it'll show you their explanation: https://regexr.com/ . Enter the regex there for it's explanation!

Sign up to request clarification or add additional context in comments.

4 Comments

i tried this regex, but still it is converting the binary value (10102 = 22)
I updated my answer, please try again, I updated the regex
thanks it works.. let me know what logic has changed after you removed //g
it's just the way the regex is created. if you create it like var re = new RegExp("([23456789])") then the blackslashes are unnecessary but if you create it like var regex = /^[0-1]*$/ then they are necessary to denote that the code is regex
1

You can use regex for this purpose: /^[0-1]*$/ will test for only 0 and 1. You can use regex.test(bin)

submit.addEventListener('click', e => {
    regex = /^[0-1]*$/;
    const bin = binary.value;
    if(bin && regex.test(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')
    } 
});

PS: There is one more bug in your code. Inside the if clause you have written bin !== '' && ' ' which is always true when bin !== '', which means && ' ' is redundent.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.