Reputation: 17
i need help please, im trying to check "if" condition inside a function and print it to the input field but i always get false, why this if statement inside the function always return false? and how can i fix it, Thanks.
<body>
<label>Value1:</label>
<input id='v1' type="number"><br><br>
<label>Value2:</label>
<input id='v2' type="number"><br><br>
<label>Value3:</label>
<input id='v3' type="number"><br><br>
<button onclick="calculateTriangle()">Answer</button>
<input id='ans'><br><br>
<script>
function calculateTriangle() {
var V1 = document.getElementById('v1').value;
var V2 = document.getElementById('v2').value;
var V3 = document.getElementById('v3').value;
console.log(V1);
console.log(V2);
console.log(V3);
var an = Triangle(V1,V2,V3)
document.getElementById('ans').value=an;
}
function Triangle(a,b,c){
if (a+b>c && a+c>b && b+c>a){
return true;
}
return false;
}
</script>
</body>
Upvotes: 1
Views: 75
Reputation: 6390
Parse your input values as an integer. Use these lines of code instead of yours. The values from your input fields are string.
So when you check a + b > c
it concatenates a + b
. Say you put a = 1
and b = 2
then it makes a + b = 12
instead of 3
.
var V1 = parseInt(document.getElementById('v1').value);
var V2 = parseInt(document.getElementById('v2').value);
var V3 = parseInt(document.getElementById('v3').value);
Upvotes: 1