Condition issue

Hi Im very stuck with I guess is very easy but I can’t find what I doing wrong… I tried to use the same condition in Node and it’s works, but when I applied in my code nothing happens.

<body>
    <h1><strong></strong>Verificador de multas</strong></h1>
    <input type="text" name="nmd" id="nmd" value="Digite">
    <input type="button" value="ir" id="ir" onclick="clicar()">
    <div id="res">
        <h2>Resultado...</h2>
    </div>
    <script>
        var idade = document.querySelector('input#nmd')
        var val = Number(idade.value)
        var res = document.getElementById('res')
        function clicar () {
        var val = 17
        if (val < 16) {
            res.innerHTML('Nao vota')
         } else if (val < 18) {
            res.innerHTML('Voto opcional')
            } else {
            res.innerHTML('Voto obrigatorio')
        }
    }
    </script>
</body>
<body>
    <h1><strong></strong>Verificador de multas</strong></h1>
    <input type="text" name="nmd" id="nmd" value="Digite">
    <input type="button" value="ir" id="ir" onclick="clicar()">
    <div id="res">
        <h2>Resultado...</h2>
    </div>
    <script>
        var idade = document.querySelector('input#nmd').value
        var res = document.getElementById('res')
        function clicar() {
            if (idade < 16) {
                res.innerHTML = `Nao vota`
            } else if (idade < 18) {
                res.innerHTML = `Voto opcional`
            } else {
                res.innerHTML = `Voto obrigado`
            }
        }
    </script>
</body>

</html>```

what value do you think this get?

Can you do a console.log(idade) and console.log(typeof idade)?

<input type="text" name="nmd" id="nmd" value="Digite">
I set to have this value of input.

No because im not trying to work with OUTPUT… i want to test in chrome.

check what is its value, it is an important thing to know how to debug

what do you think is the value of idade?

Oh I got but another example was this:

var n1 = Number(idade.value)

But I decided to put .value at the end of line… I guess I have to understand more of what is Value.

I don’t know why its just ignoring another condition… even Im putting those options

the issue is there
try to add console.log(idade) to your code
it is not what you think

But even for inside of code ?? im not trying to work with output or terminal…

the terminal is a debugging tool
are you sure that the value of idade is the one you think?
you expect idade to be a number. But is it? How do you know if you don’t print the value of idade to the console with console.log(idade)?

The problem its no show the idade, its actually to give a answer depends of what do you put into the input… Because if I only put console.log(idade) I know what its going to show me of course.

I mean… the function only recognize the first last condition the ELSE. i dont know why…

I try the debug… its saying No debug adapter, can not send ‘evaluate’

<body> <h1><strong></strong>Verificador de multas</strong></h1> <input type="text" name="nmd" id="nmd" value="Digite"> <input type="button" value="ir" id="ir" onclick="clicar()"> <div id="res"> <h2>Resultado...</h2> </div> <script> var idade = document.querySelector('input#nmd') var n1 = Number(idade.value) var res = document.getElementById('res') function clicar() { if (n1 < 16) { console.log = (Nao vota) } else if (n1 < 18) { res.innerHTML = Voto opcional } else { res.innerHTML =Voto obrigado`
}
}

`

This code:

var idade = document.querySelector('input#nmd').value
var res = document.getElementById('res')

Will execute one time when the page loads, any new value from user input is never used. Move the two lines of code into the click handler function.

Next, you have to check the length of the string, not just the value (idade.length).

Hi thanks! I try your way I guess and still not working.

<h1><strong></strong>Verificador de multas</strong></h1>
    <input type="number" name="nmd" id="nmd" value="Digite">
    <input type="button" value="ir" id="ir" onclick="clicar()">
    <div id="res">
        <h2>Resultado...</h2>
    </div>
    <script>
        function clicar() {
        var idade = document.querySelector('input#nmd').value
        var n1 = Number(idade.length)
        var res = document.getElementById('res')
            if (n1 < 70 && n1 >= 18) {
                res.innerHTML = ('VOTO OBRIGATORIO')
            } else if (n1 < 18) {
                res.innerHTML += ('VV')
            } else {
                res.innerHTML += ('VV')
            }
        }
    </script>

Even if I reduce the condition… like this >>

 function clicar() {
        var idade = document.querySelector('input#nmd').value
        var n1 = Number(idade.length)
        var res = document.getElementById('res')
            if (n1 > 18) {
                res.innerHTML = ('VOTO OBRIGATORIO')
            } else {
                res.innerHTML += ('VV')
            }
        }

THE ONLY ONE WORKING IS THE LAST – >> ELSE <<<

do you want the value of idade or its length? if you do like this n1 will just be 1 or 2

I really suggest you use console.log to print to the console the values of all your variables and check if they are what you want them to be

1 Like

do you have a console? print values to the console and debug without the debugger

1 Like

This was the only way:

 <script>
        function clicar() {
            var idade = document.querySelector('input#nmd')
            var n1 = Number(idade.value)
            var res = document.getElementById('res')
            if (n1 <= 16) {
                res.innerHTML = ('Nao vota')
            } else if (n1 <= 18) {
                res.innerHTML = ('Voto opcional')
            } else if (n1 > 18) {
                res.innerHTML = ('Voto obrigado')
            }
        }
    </script>

If you are expecting a number you should set the type attribute to number, not text. It made it unclear what you were trying to do (I thought you were looking at the number of characters, not just the value).

1 Like

Providing us with more context and expectations would help us quickly help you. Hopefully you solved.

Congrats!!

1 Like