I'm trying to create code to give me the result of the pythagorean theorem. I want to know if I'm going in the right direction so far. Any input is appreciated.
This is my code along with some comments regarding what I want to do:
function init() {
var button = document.getElementById("submit");
button.onclick = pythagoras;
}
// Computes the length of side c of a triangle, given
// the lengths of side a and b, using the Pythagorean Theorem
function pythagoras() {
// your code here to get the values of sides and b
// remember to convert a and b to numbers (using parseFloat)
function solvepy(form) {
var a = parseFloat(form.a.value);
var b = parseFloat(form.b.value);
form.c.value = Math.sqrt(a*a + b*b);
}
function pythagoras(form) {
var aInput = document.getElementById("a");
var bInput = document.getElementById("b");
}
var result = displayResult();
}
// your code here to compute the value of side c
// your code here to call the function displayResult
}
function displayResult(a, b, c) {
var div = document.getElementById("result");
div.innerHTML = "Triangle: a = " + a + ", b = " + b + ", c = " + c;
}
Use tabs to clear your code... You will better see errors !
Code:
<!doctype html>
<html lang="en">
<head>
<title> Functions </title>
<meta charset="utf-8">
<script>
window.onload = init;
function init() {
var button = document.getElementById("submit");
button.onclick = pythagoras;
}
// Computes the length of side c of a triangle, given
// the lengths of side a and b, using the Pythagorean Theorem
function pythagoras() {
// your code here to get the values of sides and b
// remember to convert a and b to numbers (using parseFloat)
function solvepy(form) {
var a = parseFloat(form.a.value);
var b = parseFloat(form.b.value);
form.c.value = Math.sqrt(a*a + b*b);
}
function pythagoras(form) {
var aInput = document.getElementById("a");
var bInput = document.getElementById("b");
}
var result = displayResult();
}
// your code here to compute the value of side c
// your code here to call the function displayResult
} // to delete
function displayResult(a, b, c) {
var div = document.getElementById("result");
div.innerHTML = "Triangle: a = " + a + ", b = " + b + ", c = " + c;
}
displayResult(); // to delete, it is to early to call this function, the document does not exist !
</script>
</head>
<body>
<form>
<label for="a">Enter lengths for sides a: </label>
<input type="text" id="a" size="3">
<label for="b"> and b: </label>
<input type="text" id="b" size="3"> <br>
<input type="button" id="submit" value="Compute c!">
</form>
<div id="result">
</div>
</body>
</html>
I replace the input type number with type text which are more usual and cross browsers...
Bookmarks