• Topic Archived
You're browsing the GameFAQs Message Boards as a guest. Sign Up for free (or Log In if you already have an account) to be able to post messages, change how messages are displayed, and view media in posts.
  1. Boards
  2. Game Design and Programming
  3. Can you show me how to validate height and width are greater than 0

User Info: LastTomorrow

LastTomorrow
10 months ago#1
<form name="Theform" id="frmcontact" method="get">
Enter the width and height of your triangle to calculate the area<br>
Width (b):<input type="text" id="width" name="txtwidth">
Height (h):<input type="text" id="height" name="txtheight">
<input type="button" value="Calculate area" id="calc" onclick="area()">
</form>

</div>

</div>

<hr>

<div id="Five">
<form name="secform" method="get">
<label for="area">The area is:</label>
<input type="text" id="solve" name="area"><br>
</form>

function area() {
var width = Theform.txtwidth.value;
var height = Theform.txtheight.value;
var area = .5 * width * height;
secform.area.value = area;
if (height < 0 && width < 0){
document.getElementById("solve").innerHTML = NaN;
}
if (height < 2 * width && width < 2 * height) {
document.getElementById("solve").innerHTML = NaN;
}
}

Trying to get NAN if I put a negative number in there.
Waffle waffle waffle

User Info: nesis

nesis
10 months ago#2
You're close! You'll want to doublecheck which logical operator you need to use in your if statements, in order for execution to go inside them.

To learn more about them in Javascript, scroll down to the "Logical Operators" section here: https://www.w3schools.com/js/js_comparisons.asp

Currently, you're using &&, the "AND" logical operator, which takes the form of A && B. For that logical operator to evaluate to true, both A and B must be true. So in your code, if A is less than 0, but B is not less than 0, the if statement's body won't execute. Similarly, if B is less than 0, but A is not less that 0, the if statement's body won't execute. Look at the logical operators in that page, and see which one suits your needs instead of && :)
|__ I make indie games! Get them for free at http://nesis.itch.io __|
|__ Let me know what you think! __________________________|

User Info: Cheezmeister

Cheezmeister
10 months ago#3
Spoiler alert: It's or ;)
I make games!
http://luchenlabs.com
  1. Boards
  2. Game Design and Programming
  3. Can you show me how to validate height and width are greater than 0
  • Topic Archived