vendredi 29 décembre 2017

I have an error in my if statement where only the if is ever run even if its false

var run = function projectileMotion() {
  var dispX = document.getElementById("displacementX").value;
  console.log(dispX)
  var dispY = document.getElementById("displacementY").value;
  var finVY = document.getElementById("finalVelocityY").value;
  var time = document.getElementById("time").value;
  var avrVX = document.getElementById("test").value;
  console.log(avrVX);
  var accelY = 9.8;


  if (dispX && finVY && dispY != "Unknown") {
    avrVX = (accelY * dispX) / finVY;
    time = dispX / avrVX;
    console.log("found it")
  } else if (dispY && avrVX && finVY != "Unknown") {
    dispX = 2 * (dispY * avrVX) / finVY;
    time = dispX / avrVX;
    console.log(2)
  } else if (finVY && dispX && avrVX != "Unknown") {
    dispY = (finVY * dispX) / (2 * avrVX);
    time = dispX / avrVX;
  } else if (avrVX && dispX && dispY != "Unknown") {
    finVY = 2 * (avrVX * dispY) / dispX;
    time = dispX / avrVX;
  } else if (dispY && avrVX != "Unknown") {
    dispX = avrVX * Math.sqrt(dispY / (0.5 * accelY));
    time = dispX / avrVX;
    finVY = 2 * (avrVX * dispY) / dispX;
    console.log("pie");
  } else if (dispX && dispY != "Unknown") {
    avrVX = dispX / (Math.sqrt(dispY / (0.5 * accelY)));
    time = dispX / avrVX;
    finVY = 2 * (avrVX * dispY) / dispX;
    console.log(39)
  } else if (dispX && avrVX != "Unknown") {
    dispY = (0.5 * accelY) * Math.pow((dispX / avrVX), 2);
    time = dispX / avrVX;
    finVY = 2 * (avrVX * dispY) / dispX;
  } else if (avrVX && finVY != "Unknown") {
    dispX = (avrVX * Math.pow(finVY, 2)) / (2 * accelY);
    time = dispX / avrVX;
    dispY = (finVY * time) / 2;
  } else if (dispX && finVY != "Unknown") {
    avrVX = ((2 * accelY) * dispX) / Math.pow(finVY, 2);
    time = dispX / avrVX;
    dispY = (finVY * time) / 2;
  } else if (dispX && avrVX != "Unknown") {
    finVY = Math.sqrt(((2 * accelY) * dispX) / avrVX);
    time = dispX / avrVX;
    dispY = (finVY * time) / 2;
  } else if (avrVX && time != "Unknown") {
    dispX = avrVX * time;
  } else if (avrVX && dispX != "Unknown") {
    time = dispX / avrVX;
  } else if (time && dispX != "Unknown") {
    avrVX = dispX / time;
  } else if (time && finVY != "Unknown") {
    dispY = (finVY * time) / 2;
  } else if (time && dispY != "Unknown") {
    finVY = (2 * dispY) / time;
  } else if (dispY && finVY != "Unknown") {
    time = (2 * dispY) / finVY;
  } else if (time != "Unknown") {
    dispY = (0.5 * accelY) * Math.pow(time, 2);
    finVY = accelY * time;
  } else if (dispY != "Unknown") {
    time = Math.sqrt(dispY / (0.5 * accelY));
    finVY = accelY * time;
  } else if (finVY != "Unknown") {
    time = finVY / accelY;
    dispY = (0.5 * accelY) * Math.pow(time, 2);
  } else {
    console.log("invalid")
  }
  document.getElementById("horizontalDisplacementInsert").innerHTML = dispX;
  document.getElementById("verticalDisplacementInsert").innerHTML = dispY;
  document.getElementById("horizontalVelocityInsert").innerHTML = avrVX;
  console.log(avrVX);
  document.getElementById("finalVerticalVelocityInsert").innerHTML = finVY;
  document.getElementById("timeInsert").innerHTML = time;

};
document.getElementById("inputCheckSubmit").onclick = run;
form {
  width: 400px;
  float: left;
}

#answersDiv {
  float: left;
}
<!DOCTYPE html>
<html>

<head>
  <title>
    Projectile Motion
  </title>
  <link rel="stylesheet" type="text/css" href="Projectile Motion.css"></link>
  <script>
  </script>
  <style>

  </style>
</head>

<body>
  <form method="post">
    <fieldset>
      <legend class="inputCheck" id="inputCheckLegend">Which of these do you know the value of?</legend>
      Reply "Unknown" for variables that you don't know
      <br/>
      <br/>
      <h2>
        Time
      </h2>
      <input class="inputCheck" id="time" type="text" name="time">
      <br/>
      <h2>
        Horizontal (x-axis)
      </h2>
      Displacement
      <br/>
      <input class="inputCheck" id="displacementX" type="text" name="displacementX">
      <br/> Velocity
      <br/>
      <input class="inputCheck" id="test" type="text" name="velocityX">
      <br/>
      <h2>
        Vertical (y-axis)
      </h2>
      Displacement
      <br/>
      <input class="inputCheck" id="displacementY" type="text" name="displacementY">
      <br/> Final Velocity
      <br/>
      <input class="inputCheck" id="finalVelocityY" type="text" name="fianlVelocityY">
      <br/>
      <input class="inputCheck" id="inputCheckSubmit" type="button" value="Submit" />
    </fieldset>
  </form>
  <div class="answersDiv" id="answersDiv">
    <h2 class="answers, answerPrompt" id="horizontalAccelerationAnswer">
      Horizontal Acceleration = 0.00
    </h2>
    <h2 class="answers, answerPrompt" id="verticalAccelerationAnswer">
      Vertical Acceleration = 9.81
    </h2>
    <h2 class="answers, answerPrompt" id="horizontalDisplacementAnswer">
      Horizontal Displacement =
      <span id="horizontalDisplacementInsert"></span>
    </h2>
    <h2 class="answers, answerPrompt" id="verticalDisplacementAnswer">
      Vertical Displacement =
      <span id="verticalDisplacementInsert"></span>
    </h2>
    <h2 class="answers, answerPrompt" id="horizontalVelocityAnswer">
      Horizontal Velocity =
      <span id="horizontalVelocityInsert"></span>
    </h2>
    <h2 class="answers, answerPrompt" id="initialVerticalVelocityAnswer">
      Initial Vertical Velocity = 0.00
    </h2>
    <h2 class="answers, answerPrompt" id="finalVerticalVelocityAnswer">
      Final Vertical velocity =
      <span id="finalVerticalVelocityInsert"></span>
    </h2>
    <h2 class="answers, answerPrompt" id="timeAnswer">
      Time =
      <span id="timeInsert"></span>
    </h2>
  </div>
</body>
<script src='http://ift.tt/29q98ga'></script>
<script src="Projectile Motion.js"></script>

</html>

I have no clue what the error is please console.log()'s were only to help me find the error

Aucun commentaire:

Enregistrer un commentaire