JavaScript: The result is displayed even without data entered by the user!

0

Well, the idea was to do a simple function that calculates the value entered in an input and returns the result to the user, but if the value entered is zero, the message that returns is: "Value in White!"; (Because the "" being converted to number becomes equal to zero). In short, there is a way for this to work there or I'll have to rewrite it in a different way.

function inp() {

    var num = Number(input.value);
    var myNum = (num == " ");

    if (Number.isNaN(num) == true) {
        alert("Formato Inválido!");
    } else if (myNum === true) {
        alert("Valor em branco!")
    } else {
        alert((50 + 50 + 50) * num);
    }
}
    
asked by anonymous 27.03.2018 / 23:34

1 answer

0

You can use a ternary to set num :

function inp() {

    var input = document.querySelector("#ip");
    var num = input.value == '0' ? '0' : Number(input.value);
    var myNum = (num == " ");

    if (Number.isNaN(num) == true) {
        alert("Formato Inválido!");
    } else if (myNum === true) {
        alert("Valor em branco!")
    } else {
        alert((50 + 50 + 50) * num);
    }
}
<input type="text" id="ip" />
<input type="button" value="ok" onclick="inp()" />
    
27.03.2018 / 23:53