How to return with an error Alert, when you do not enter a value in the variable?

Asked

Viewed 19 times

1

I was doing some exercises to practice Javascript, when I came across a doubt, how do I fire an error Alert when at least one of the variables receives a string or no value?

That’s the code I created.

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Diferença</title>
    <style>
        body{
            font: normal 80px Arial;
        }
    </style>
</head>
<body>
    <script>
        var A = Number.parseInt(window.prompt('Digite o valor de A:'))
        var B = Number.parseInt(window.prompt('Digite o valor de B:'))
        var C = Number.parseInt(window.prompt('Digite o valor de C:'))
        var D = Number.parseInt(window.prompt('Digite o valor de D:'))
        var dif = (A*B)-(C*D)
        document.write(`DIFERENÇA = ${dif}`)
    </script>
</body>
</html>
  • Beyond the isNaN you should also compare with empty string if (a === ''), because when the user does not type anything, this value is an empty string.

No answers

Browser other questions tagged

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