0

I'm trying to write a script to accept at maximum 8 binary elements, verify if they are indeed binary and show they representation in decimal numbers. I'm stuck in the verification. I'm pretty new at programming and I don't know how to solve this problem.

function verify() {
    var numBin = document.getElementById('bin-input')
    var c = 0
    if (numBin.value == "" || numBin.value.length > 8 || isNaN(numBin) == "True"){
        alert('Write only 8 binary elements!')
    } else (while (numBin.value.length != c){
        if (numBin.length[c] == '0'|| numBin.length[c] == '1'){
            c ++
        } else {
            break
        }
        }){
            alert('Write only 0 and 1')
        }

}

This is what I've made so far but the while inside the else if isn't working and I don't know how to fix it. How can I fix this issue?

2
  • 1
    isNaN(numBin) == "True" <-- I think you need to read the documentation for isNaN Commented Feb 18, 2020 at 16:22
  • Welcome to SO! You're missing a parenthesis on the (while (numBin.value.length != c) block. Remove == "True"--isNaN(numBin) is enough to test a boolean. Other than that, what isn't working specifically? How should it work? Commented Feb 18, 2020 at 16:22

1 Answer 1

1

Here's an example how it could work, if you have any question to specific code lines let me know :)

function verifyAndConvert() {
  let input = document.getElementById('bn').value;
  if (verify(input)) {
    convert(input);
  }
}

function verify(input) {
  var numBin = input;
  var c = 0;
  var binaryFormat = /^[0-1]+$/;
  if (numBin.length > 8 || isNaN(numBin)) {
    alert('Write no more than 8 binary elements!');
  } else if (!numBin.match(binaryFormat)) {
    alert('Write only 0 and 1');
  } else {
    return true;
  }
  return false;
}

function convert(input) {
  alert(parseInt(input, 2));
}
<label for="fname">Binary Number:</label>
<input type="text" id="bn">
<button onclick="verifyAndConvert()">Verify and Convert</button>

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

4 Comments

Thank you so much! Couldn't have thought such a solution!!
But I indeed have some questions. First, what is the purpose the first "function"? Couldn't I do just a "var numBin = document.getElementById('bn').value? And in "var binaryFormat", what is this pattern you coded? In the "else if", why is there an exclamation before numBin? And last but not least, the function convert(input) cant be inside the verify(input) function?
I like to keep functions as small as possible in order to get a better overview. In this case it doesn't matter much - but in bigger projects it can be very useful to split functions into smaller reusable ones. binaryFormat is a regex pattern that checks if there are only 0s and 1s inside the string. The exclamation negates the result - so if the pattern matches the return is true, but the exclamation negates it to false. Yes, the functions could be anywhere. It's just a matter of programming style - I just try to keep it as simple as possible.
Damn, you're good ma'am, thank you so much, it openned my mind to some ways to code that I couldn't thought, again, thank you so much!! ^.^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.