Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by dizzyniho

Hey guys I'm lerning js but i think there is something wrog with my code. It compares two should compare two numbers.

<!DOCTYPE HTML>
<html>
	<head>
		<title> Formulare </title>
	</head>
	<body>
		<script type="text/javascript">
		
			function Ausgabe() {
				a = document.getElementById('Nr1').value;
				b = document.getElementById('Br2').value;
				if (a < b) {document.write}
			}

		</script>
		<form> 
			
			Wert 1: <input type="text" name="Feld1" id="Nr1"><br>
			Wert 2: <input type="text" name="Feld2" id="Nr2"><br><br>

			<input type="button" value="Berechnung" onclick="Ausgabe()">

		</form>
	</body>
</html>	

1 decade ago by dizzyniho

function Ausgabe() {
				a = document.getElementById('Nr1').value;
				b = document.getElementById('Br2').value;
				if (a < b) {document.write(a + " is smaller than" + b);}
				else if (a == b) {document.write(a + "is simulare to" + b);}
				else {document.write(a + "is bigger than" + b)}
			}

1 decade ago by ryph_z

The script is getting the element Br2, but the form input ids are named Nr1 and Nr2.

1 decade ago by Joncom

Also, getElementById fetches values as strings, so you will want to parse the numbers.

// example
a = parseFloat(document.getElementById('Nr1').value);

Otherwise you are just doing an alphabetical comparison, like "apple" < "orange", which equals true because "a" comes before "o".
Page 1 of 1
« first « previous next › last »