0

I have to convert from decimal to binary and it requires at least 3 functions and it has to be displayed in HTML. This is what I've got so far and can't figure out how to make it display properly?

// Prompt user for number between 1-1000
let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));

function store() {
let quotient = [];
let answer = quotient.reverse();
return answer;
}
function check() {
    while (input != 0) {
        if (input < 1000 && input % 2 != 0) {
            return quotient.push("1");
            input = input / 2;
        }
        else if (input < 1000 && input % 2 == 0) {
            return quotient.push("0");
            input = input / 2;
        }
    }
}
function display() {
    document.getElementById("displayNumber").innerHTML = answer;
}
display();
<h1 id="displayNumber"></h1>

4 Answers 4

1

Here is the fixed script, I hope this helps.

   function check(input, quotient) {
       while (input != 0) {
        if (input < 1000 && input % 2 != 0) {
            quotient.push("1");
            input = parseInt(input / 2);
        }else if (input < 1000 && input % 2 == 0) {
            quotient.push("0");
            input = parseInt(input / 2);
        }
    }
}

function display() {
    let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
    let quotient = [];
    check(input, quotient);
    let answer = quotient.reverse().join('');
    document.getElementById("displayNumber").innerHTML = answer;
}
display();
Sign up to request clarification or add additional context in comments.

Comments

0

Currently, you are just creating the function store and check but not actually calling them.

However, if all you are wanting to do is to display the input as binary, you can use the toString function, passing in the base you desire.

Im not sure what you want to display if the number is outside of the range 1-1000. So I just put "Invalid input". You can add some more checks for if it is NAN ect

 <!DOCTYPE html>
 <html>

   <h1 id="displayNumber"></h1>
   <script>
     // Prompt user for number between 1-1000
     let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
     
     function display() {
       if (input < 1 || input > 1000) {
         document.getElementById("displayNumber").innerHTML = "Invalid input";
       } else {
         document.getElementById("displayNumber").innerHTML = input.toString(2);
       }
     }

     display();

   </script>
 </html>

1 Comment

if I want to make use of the other functions, how should I go about doing it?
0

Here is updated snippet :

		function check(input) {
  	let quotient = [];
             while (input != 0) {
                 if (input < 1000 && input % 2 != 0) {
                     input = input / 2;
                 }
                 else if (input < 1000 && input % 2 == 0) {
                     input = input / 2;
                 }

                   quotient.push(input);
             }
             return quotient;

         }
 
         function display() {
             let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
             var answer = check(input);
             document.getElementById("displayNumber").innerHTML = answer;         

        }          
        display();
 <h1 id="displayNumber"></h1>

2 Comments

If i wanted to add one more function, could I make it so I have a function with the array? How would I call it from there though?
You can make numbers of functions if you can and added as per requirements
0

In display function, you are assigning answer to innerHTML but not calling your check function.

<html> 
    <h1 id="displayNumber"></h1>
    <script>
        let input = parseInt(prompt('Enter a number between 1 and 1000', '50'));
        function check(input) {
            let quotient = [];
            while (input > 0) {
                quotient.push(input % 2);
                input = parseInt(input / 2);
            }
            return quotient.reverse().join('');
        }

        function display() {
            if (input >= 1 && input <= 1000) {
                document.getElementById("displayNumber").innerHTML = check(input);
            }
        }

    display();

</script>
</html>

Comments

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.