Having trouble displaying my number using javascript

Related searches

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>

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>

Basic math in JavaScript — numbers and operators, You'll see numbers used again and again, all the way through your JavaScript At this point in the course we discuss math in JavaScript — how we can use We've actually provided a simple example showing how this is done — check it Troubleshooting JavaScript � Storing the information you need —� Indeed, many of JavaScript’s subtleties lead to a number of common problems that keep it from working – 10 of which we discuss here – that are important to be aware of and avoid in one’s quest to become a master JavaScript developer. Common Mistake #1: Incorrect references to this. I once heard a comedian say:


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>

Working with Phone Numbers in JavaScript, This article explores the pitfalls associated with working with phone Email addresses, while theoretically very easy to validate, have their own challenges around collecting, validating, and displaying telephone numbers. Let's look at some of the other issues around telephone numbers, and why they� Learn how you can effectively format the phone number in javascript. Example Input: 9809142333 Output: (980) 914-2333 Format phone number without country code. To format phone number properly we need to first make sure that the input is numeric and is not more than 10 numbers. Now once we have our input ready we can format it in US phone format.


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

JavaScript Numbers, This format stores numbers in 64 bits, where the number (the fraction) is var myNumber = 2; By default, JavaScript displays numbers as base 10 decimals. JavaScript has a reputation for being one of the worst programming languages in existence, and for good reasons! JavaScript is easy to learn and easy to use, except when it’s not. There are many…


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>

JavaScript Debugging, But fortunately, all modern browsers have a built-in JavaScript debugger. If your browser supports debugging, you can use console.log() to display JavaScript� JavaScript doesn’t distinguish between whole numbers and decimals, so you can use them together without having to convert from one to the other. EXAMPLE 10 + 3.14159;


JavaScript Performance, How to speed up your JavaScript code. Reduce Activity in Loops. Loops are often used in programming. Keep the number of elements in the HTML DOM small. and speed up rendering (page display), especially on smaller devices. In addition, the Firebug add-on can help diagnose JavaScript problems. In Internet Explorer, your status bar will display a warning if any JavaScript errors are discovered on the page. Double-click the warning to see the problem code. In Safari, use the Elements tab of Web Inspector to check for JavaScript problems.


Why am I having problems sharing my screen during a Skype call? If you are having issues sharing your screen in Skype, the following tips may help.The screen sharing options are grayed out.You need to be in a voice or video


Having an issue with your new Samsung Galaxy S20, S20 Plus, or S20 Ultra? We take a look at some of the most common problems and teach you how to fix them.