Reputation: 57
So my code just adds them together if i put like 4+4 it gives me 44 but i wanted to be like 4+4=8
<html>
<head>
<title>Laskeminen</title>
<body>
<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>
<p id="first,last"></p>
<script>
var y = first
var z = last
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script>
</head>
</body>
</html>
Upvotes: 1
Views: 444
Reputation: 757
<html>
<head>
<title>Laskeminen</title>
<body>
<p id="first,last"></p>
<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
var y = parseInt(first);
var z = parseInt(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script>
</head>
</body>
</body>
</html>
Upvotes: 0
Reputation: 30739
You can use parseFloat()
on the values to convert it into mathematical float values that can perform mathematical operation:
var first = prompt("Enter your first number.");
var last = prompt("Enter your second number.");
var y = parseFloat(first);
var z = parseFloat(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
<p id="first,last"></p>
But if you want only integers
then you can do parseInt()
:
var first = prompt("Enter your first number.");
var last = prompt("Enter your second number.");
var y = parseInt(first);
var z = parseInt(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
<p id="first,last"></p>
Upvotes: 0
Reputation: 1782
In your second addition script you need to parse your input because currently it is a string. See below
first = prompt("Enter your first number.");
console.log('User input is: ', typeof first)
so because these are string when your adding text they get added together e.g.
var text = 'ab';
var text1 = 'c';
if(text+text1 == 'abc'){
console.log('These have been added together and are equal')
}
So you need to parse now if your dealing with ints or whole numbers you can use parseInt(**variabletoParse**)
or parseFloat(**variabletoParse**);
So what you need to do can be seen blow:
<html>
<head>
<title>Laskeminen</title>
<body>
<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>
<p id="first,last"></p>
<script>
var y = parseFloat(first);
var z = parseFloat(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script>
</head>
</body>
</html>
Upvotes: 0
Reputation: 701
Use Number()
:
<html>
<head>
<title>Laskeminen</title>
<body>
<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>
<p id="first,last"></p>
<script>
var y = Number(first);
var z = Number(last);
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script>
</head>
</body>
</html>
Upvotes: 1
Reputation: 284
You can use parseInt()
<html>
<head>
<title>Laskeminen</title>
<body>
<script language="JavaScript">
first = prompt("Enter your first number.");
last = prompt("Enter your second number.");
</script>
<p id="first,last"></p>
<script>
var y = parseInt(first)
var z = parseInt(last)
var x = y + z;
document.getElementById("first,last").innerHTML = x;
</script>
</head>
</body>
</html>
Upvotes: 1