简体   繁体   中英

Having trouble displaying my number using javascript

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>

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();

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>

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM