I’m not being able to print javascript

Asked

Viewed 44 times

-2

Well I’m trying to make a simple html page where the user type a number and then the page shows the Full of it. Only I’m not getting the page to type Abuda, someone can help me?

<header><h3>Tabuada</h3></header>
        <p><input type="number" name="n" id="n" placeholder="Digite um numero"></p>
        <p><button type="submit" onclick="tabuada()">Executar</button></p>
        <div id='tab' style='display:inline'></div>
    <script>
        function tabuada(){
            var resultado = document.getElementById('tab');
            var text = '';
            var num = parseInt(document.getElementById("n").value);
            for(var x=1; x<=10; x++){
                text += num+"x"+i+"="+num*i+"<br />";
            }
            resultado.innerHTML = text;
        }
    </script>
</body>
</html>

2 answers

1

In your "go" you declared the counter as "x" but you’re using it as if it were "i".

The correct would be to change the "for" of:

for(var x=1; x<=10; x++)

To:

for(var i=1; i<=10; i++)

The whole code would look like this:

<header><h3>Tabuada</h3></header>
        <p><input type="number" name="n" id="n" placeholder="Digite um numero"></p>
        <p><button type="submit" onclick="tabuada()">Executar</button></p>
        <div id='tab' style='display:inline'></div>
    <script>
        function tabuada(){
            var resultado = document.getElementById('tab');
            var text = '';
            var num = parseInt(document.getElementById("n").value);
            for(var i=1; i<=10; i++) {
                text += num+"x"+i+"="+num*i+"<br />";
            }
            resultado.innerHTML = text;
        }
    </script>
</body>
</html>

-1


When executing your code, you can see the error by clicking the button executar.

Uncaught Referenceerror: i is not defined

To solve this problem just use variable x.

This to fix this problem just use the variable that is set in the loop loop loop body for. In your case is the variable x.

for(var x=1; x<=10; x++){
   text += num+"x"+x+"="+num*x+"<br />"; //mudar 'i' para 'x'
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.