1
I’m trying to make a calculator in Javascript/HTML, but it’s giving an error of NaN
in user entries, even using a parseFloat
, he still says it’s not a number, someone could tell me how to solve it?
var num1 = parseFloat(document.getElementById("n1").value);
var num2 = parseFloat(document.getElementById("n2").value);
function calculate(op) {
var opV = document.getElementById("op");
var operator = op.value;
opV.value = operator;
var res = 0;
res = eval(num1 + operator + num2);
var resV = document.getElementById("result");
resV.value = res;
}
<!DOCTYPE html>
<html lang="pt_br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link href="https://fonts.googleapis.com/css2?family=Lexend+Tera&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAsVBMVEX///+Vpabs8PE0SV5/jI0sPlCRoqPFzM3nTDyZqamNnJ29xscwQ1fb4eI3SFcwRlxbbHmhr7Dx9faHlZbo7O12hIXQ19gnOk3s9fYcMkefp6znRzZOXGpOYXDpjoboenHnV0jppJ/qura3vcLnQC0hNUmyvb6stLoTLEJzg4lqeoFcbXbJ0dKHmJs+UmV6i5EAITrrzcsAFDLnOSOMlZ3plo+psLYJJz7ogHbocmfr1dMKW/CjAAAIhklEQVR4nO2da2ObNhSGAwG5mBAHmUudOuvaBGK7zmVb1677/z9sgMHB5kiWYt3s6e2nOrbQwxHSQdILFxdWVlZWVlZWVqCSaD0KVWu0jhI1eFHpIF1yykg6XxZUx9EnhIJMKl8S68RrIWOJjTXTGr9OCEkL48gEvlpodOaAshAzcwArRAkN1TUJsEJ0hRMa0Iv2hWLRgJFZgBWi6LE/1k00kOAgJqaFsAqi2IE/NJAwFEpoXiMV3EyxeSGsgogFEhp4GQq+EI3KZzoJzWvIhIF8KSFcw4SBs7y7lq27pQNDorV8wlVe5LkvV3l1jJUmQjQvJNN1Kubg8WUTBsoAa0SgocomDJbqACvE5RBRNiG6Ugjo+1dADSQTvhRKCYsX1YTBN9md6K7yb4Nm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ggYY/wcafoKRRJCBp5mRGJ+7yPAXQ8kYQjIK9kRqRYLJkMlgRAlAq9DoG0kLGGU/ojE8YMJcBnaCqYkNzSjnwp2XtLQJ5QwnVKsNTrU5UMiRzxMzD11ak68c8EErp1cki/oNSqHoxTVyAhbvoDcxCbbGOKBRJexOReW4M2/W8sErC5EOlDmzq1Q5TQy7ASdfBVqu0AKhbwIky3jDrj+JZipCJztkY7WcZ0rEPTnfxHNGAz6BskocN9q9gkxFRsR9qK89lXUjWWAVgN+6ZEMRU72Pc0NgMxlRPBRqUJiGkpD/DiIkG6GVOUyASsFMVvE9nK6dI0jiTz1cJRGXPOeAvRNC6jd/Qw/wHdNfwdNOd19wAAAABJRU5ErkJggg==" type="image/x-icon">
<style>
* {
font-family: 'Lexend Tera', sans-serif;
}
body {
background: white;
}
div#calculator {
margin: 0;
margin-top: 50px;
width: 400px;
height: 150px;
padding: 50px;
background-color: rgb(209, 209, 209);
border-radius: 15px;
margin-left: 30%;
}
input[name=operator] {
width: 30px;
height: 25px;
text-align: center;
}
input[type=number] {
width: 150px;
height: 25px;
border: none;
background-color: rgb(221, 221, 221);
}
button[id=opr] {
width: 175px;
border:1px;
}
span {
background-color: rgb(209, 209, 209);
padding: 10px;
border-radius: 50px;
padding-left: 20px;
padding-right: 80px;
margin-left: 30%;
padding-bottom: 20px;
padding-top: 100px;
}
input[id=result] {
margin-top: 10px;
width: 350px;
height: 30px;
}
</style>
</head>
<body>
<span>
Calculator<br>
</span>
<div id="calculator">
<header>
<input type="number" name="number1" id="n1" placeholder="Enter a number">
<input type="text" name="operator" id="op" value=" " disabled>
<input type="number" name="number2" id="n2" placeholder="Enter a number">
</header>
<main>
<button name="+" id="opr" value="+" onclick="calculate(this)">+</button>
<button name="-" id="opr" value="-" onclick="calculate(this)">-</button>
<button name="x" id="opr" value="*" onclick="calculate(this)">x</button>
<button name="/" id="opr" value="/" onclick="calculate(this)">/</button>
</main>
<input type="text" id="result" disabled placeholder="Result">
</div>
</body>
</html>
has already tried to place the two lines within the function?
– user60252
@Leocaracciolo was worth, nor thought it could be that, it worked.
– GabrielBellasco
About the use of
eval
, is worth reading here and here– hkotsubo
Okay thanks @hkotsubo
– GabrielBellasco