JavaScript code does not work as expected

how to solve browser compatibility issues in javascript
common challenges with javascript
javascript issues
javascript not working in firefox but working in chrome
javascript is not a function
javascript function return undefined
javascript not running on page
typeerror is not a function javascript

So I made this little thing as I am quite new to programming, but when I open it in Chrome, I am able to type input but then nothing happens. Does anyone know how I can fix this code? Thanks in advance!

    <!DOCTYPE html>
    <html>
    <head>
    	<title>Number Guessing</title>
    </head>
    <body>
    	<b id="bold">Guess:</b> <input type="text" id="guess">
    	<input type="submit" value="GO!">
    	<script>
    		function startGame() {
    			function getRandomNumber(low, high) {
    				var number = Math.floor(Math.random() * (high - low +1)) + low;
    				return number;
    			}
    			var number = getRandomNumber(1,10);
    			var guess = document.getElementById("guess");
    			for (var i=0;i=0) {
    				if (guess>number) {
    					guess = document.getElementById("guess");
    					document.getElementById("bold").innerHTML = "You're too high, try lower!";
    				}
    				if (guess<number) {
    					guess = document.getElementById("guess");
    					document.getElementById("bold").innerHTML = "You're too low, try higher!";
    				}
    				if (guess==number) {
    					alert("You're correct, the number is "+number+"!!!");
    					alert("Thanks for playing my game and have a good day!");
    				}
    			}	
    		}
    		startGame();
    
    </script>
    </body>
    </html>

You've got a lot of problems, starting with a syntax error.

You have a submit button, but no form to submit. You really just need a button. But, even then, you have to set up a click event handler for it.

Then, your loop isn't configured properly.

You also are not accessing the data the user has typed into the textbox correctly - you need to get the value of the element.

Your if statements should be else if.

The b element should not be used just for presentation. HTML is a "semantic" language, meaning that you use a tag to describe the meaning (not presentation) of an element. For styling use CSS.

See comments inline below for details.

/* CSS is for presentation, not HTML */
#bold { font-weight:bold; }
<!DOCTYPE html>
<html>
<head>
    <title>Number Guessing</title>
</head>
<body>
    <!-- Don't use HTML for styling, use it for semantics. -->
    <span id="bold">Guess:</span> <input type="text" id="guess">
    
    <!-- You need a <form> if you have a submit button. For this, you just want a button. -->
    <input type="button" value="GO!" id="go">
    <script>
        function startGame() {
            function getRandomNumber(low, high) {
                var number = Math.floor(Math.random() * (high - low + 1)) + low;
                return number;
            }
            var number = getRandomNumber(1,10);
            var guess = document.getElementById("guess");
            
            // Get a reference to the output area just once
            var output = document.getElementById("bold");
            
            // Give the user 3 tries. Your loop wasn't configured properly.
            for (var i=0; i < 3; i++) {
                // You want to access the data in the textbox. That's the value
                // Also, if the first condition isn't true, try the next and so on.
                // This is done with else if branches
                if (guess.value > number) {
                    output.textContent = "You're too high, try lower!";
                } else if (guess.value < number) {
                    output.textContent = "You're too low, try higher!";
                } else if (guess.value == number) {
                    alert("You're correct, the number is "+number+"!!!");
                    alert("Thanks for playing my game and have a good day!");
                    break; // Get out of the loop because the game is over.
                }
            }   
        }
        
        // Set it up so that clicks on the button run the function
        document.getElementById("go").addEventListener("click", startGame);

</script>
</body>
</html>

Why this javascript code does not work as expected?, When the click listener is executed, pos has the value of 3, your code is fine, look at this example: function koko() { items = document. As discussed in our JavaScript Hiring Guide, a common source of confusion among JavaScript developers (and therefore a common source of bugs) is assuming that JavaScript creates a new scope for each code block. Although this is true in many other languages, it is not true in JavaScript. Consider, for example, the following code:

you have some errors:

this doesnt work, it wont loop. actually, why do you want to loop?

for (var i=0;i=0) {

this will run the function once, this means when the user writes the value it wont be checked

startGame();

the button doesnt do anything, also it has a submit and you don't have any forms:

input type="submit" value="GO!">

on each if, the conditions are exclusive, use if/else

below is a working code:

<!DOCTYPE html>
<html>

<head>
  <title>Number Guessing</title>
</head>

<body>
  <b id="bold">Guess:</b> <input type="text" id="guess">
  <input value="GO!" onclick="checkGuess()">
  <script>
    var number = 0;

    function startGame() {
      function getRandomNumber(low, high) {
        var number = Math.floor(Math.random() * (high - low + 1)) + low;
        return number;
      }
      number = getRandomNumber(1, 10);
    }

    function checkGuess() {
      var guess = document.getElementById("guess").value;
      if (guess > number) {
        guess = document.getElementById("guess");
        document.getElementById("bold").innerHTML = "You're too high, try lower!";
      } else if (guess < number) {
        guess = document.getElementById("guess");
        document.getElementById("bold").innerHTML = "You're too low, try higher!";
      } else if (guess == number) {
        alert("You're correct, the number is " + number + "!!!");
        alert("Thanks for playing my game and have a good day!");
      }
    }
    startGame();
  </script>
</body>

</html>

Buggy JavaScript Code: The 10 Most Common Mistakes , If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common Uncaught TypeError: undefined is not a function whoAmI(); // outputs "MyObj" (as expected) obj.w(); // outputs "MyObj" (as expected)  Prolog print declaration does not work as expected I was trying to define a functor and print each individual items of list in Prolog, but Prolog is not printing in correct format. rint(L):- write(H). the output is like rint([a, s,v ,c]).

Although i have no idea about what your program does. you have a syntax error at

for (var i=0;i=0) {

and also you should bind an event to that button rather than doing a submit.

Handling common JavaScript problems, Now we'll look at common cross-browser JavaScript problems and in their code, and find that such features don't work in older browsers. try to access jsonObj (which as you might expect, is supposed to be a JSON object)  I am learning javascript language and so I am on a project while I came across this issue. Why this javascript code does not work as expected? Posting code

JavaScript Mistakes, This if statement returns false (as expected) because x is not equal to 10: var x = 0; if (x == 10) Try it Yourself ». The function will return undefined ! Why? A code editor that includes JavaScript code hints and code coloring can be a very valuable tool when looking for misspelled words. Some code editors will apply a special color to function names that are spelled correctly, while coloring misspelled names differently.

.join not working as expected - JavaScript, I'm trying to understand a palindrome problem and I have written the code below. Why does .join not work by itself but works when embedded  setTimeout(func, 0) will not work as expected. performance. javascript. If you're trying to start a thread (or do something asynchronously) in Javascript then setTimeout(func, 0) and setInterval(func, 0) are not really the way to go since the HTML5 spec defines the lowest value allowable as 4ms.

Window.print() Is Not Working As Expected, I have a simple HTML code to print the page. Below is the code: !DOCTYPE html html head script function printPage() { var w  While VS Code does not include a built-in JavaScript linter, many JavaScript linter extensions available in the marketplace. Tip: This list is dynamically queried from the VS Code Marketplace . Read the description and reviews to decide if the extension is right for you.

Comments
  • What do you think should happen? You have a submit button but no form.