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 stringif (a === '')
, because when the user does not type anything, this value is an empty string.– Rafael Tavares