jeudi 21 mai 2015

javascript trigonometry if statement

I have almost completed a javascript program for finding unknowns in triangles using trigonometry, but my most recent addition to the program isn't working and is messing up the program, preventing it from answering. You can put it in a html to check it, and if you can make the code block work, and you are doing trig, feel free to keep the code xD and answer. Thanks

<script language="JavaScript">
function isNumber(obj) {
    return !isNaN(parseFloat(obj))
}
var opposite = "x"
var adjacent = "x"
var hypotenuse = "x"
var angle1 = "x"
var angle2 = "x"
var count = 0

alert("Type in anything that isn't a number for the unknowns (you can leave it blank)")
hypotenuse = prompt("What is the hypotenuse")
opposite = prompt("What is the opposite side")
adjacent = prompt("What is the adjacent side")
angle1 = prompt("What is the angle relative to the above information")
angle2 = prompt("What is the other angle")

if (isNumber(adjacent) == 0)
    adjacent = "x"
if (isNumber(opposite) == 0)
    opposite = "x"
if (isNumber(hypotenuse) == 0)
    hypotenuse = "x"
if (isNumber(angle1) == 0)
    angle1 = "x"
if (isNumber(angle2) == 0)
    angle2 = "x"

while ((isNumber(opposite) == 0) || (isNumber(adjacent) == 0) || (isNumber(hypotenuse) == 0) || (isNumber(angle1) == 0) || (isNumber(angle2) == 0)) {

    //Find angle1
    if (isNumber(opposite && hypotenuse)) {
        angle1 = (Math.asin(opposite / hypotenuse) * (180 / Math.PI))
    } else if (isNumber(adjacent && hypotenuse)) {
        angle1 = (Math.acos(adjacent / hypotenuse) * (180 / Math.PI))
    } else if (isNumber(opposite && adjacent)) {
        angle1 = (Math.atan(opposite / adjacent) * (180 / Math.PI))
    }

    //Find missing side and angle2
    if ((isNumber(hypotenuse) == 1) && (isNumber(opposite) == 1)) {
        adjacent = Math.sqrt(Math.pow(hypotenuse, 2) - Math.pow(opposite, 2))
        angle2 = (Math.asin(adjacent / hypotenuse) * (180 / Math.PI))
    } else if ((isNumber(hypotenuse) == 1) && (isNumber(adjacent) == 1)) {
        opposite = Math.sqrt(Math.pow(hypotenuse, 2) - Math.pow(adjacent, 2))
    } else if ((isNumber(adjacent) == 1) && (isNumber(opposite) == 1)) {
        hypotenuse = (Math.sqrt(Math.pow(opposite, 2) + Math.pow(adjacent, 2)))
    }

    //THIS IS THE STUFFY CODE: It finds the sides from the angles
    if ((isNumber(hypotenuse) == 1) && (isNumber(angle1) == 1)) {
        opposite = (sin(angle1) * hypotenuse)
        adjacent = (cos(angle1) * hypotenuse)
    }
    if ((isNumber(opposite) == 1) && (isNumber(angle1) == 1)) {
        hypotenuse = (opposite / sin(angle1))
        adjacent = (opposite / tan(angle1))
    }
    if ((isNumber(adjacent) == 1) && (isNumber(angle1) == 1)) {
        opposite = (tan(angle1) * adjacent)
        hypotenuse = hypotenuse = adjacent / cos(angle1)
    }

    //Error detection
    count++
    if (count > 5) {
        alert("Insufficient information, please try again")
        throw new Error("Something went badly wrong!")
    }
}
alert("The first angle is " + (angle1) + ".\nThe opposite side is " + opposite + ".\nThe adjacent side is " + adjacent + ".\nThe hypotenuse is " + hypotenuse + ".\nThe second angle is " + angle2 + ".")

Aucun commentaire:

Enregistrer un commentaire