-4

Attempting to have the user input four numbers, where they are, and their destination. Then have the output which direction they will be heading.
No matter which values I enter the same thing happens. How can I make the code use the results of the prompts?

var output = document.getElementById("output");
var number=prompt ("What is your current Latitude?");
var number=prompt ("What is your current Longitude?");
var number=prompt ("What is your destination Latitude?");
var number=prompt ("What is your destination longitude?");


var intCurrentLatitude = 0;
var intCurrentLongitude = 0;
var intDestinationLatitude = 0;
var intDestinationLongitude = 0;

if ( (intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
output.textContent = "We'd be headed North East, capt'n!";
}
else if ( ( intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
    output.textContent = "Ye'd best head North West, captain!";
}
else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
output.textContent = "Ye'd best head South West, captain!";
}
else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
output.textContent = "Ye'd best head South East, captain!";
}
else{
output.textContent = "Land Ho!";
}

HTML

<head>
<meta charset="utf-8">
  <title>More if</title>

<body>
 <div id= "output">


 <div id= "input2">
    </div>

</body>


<script src="moreif.js"></script>
<script src="moreif2.js"></script>
</head>
</html>
albert
  • 8,112
  • 3
  • 47
  • 63
jjdouble000
  • 93
  • 1
  • 7

2 Answers2

2

I'm not sure what you're trying to accomplish, but the code is doing exactly what it's supposed to. Each of your conditions are asking if a variable is "less than or equal to zero".

The variables you're testing are all at 0, so the first condition fulfills and your program exits.

Phillip Chan
  • 993
  • 7
  • 17
1

You kept changing the number value and prompt returns a string. Use parseFloat to convert a decimal in string form to a number. You did not use the result of the prompts.

var output = document.getElementById("output");

var intCurrentLatitude = parseFloat(prompt("What is your current Latitude?"));
var intCurrentLongitude = parseFloat(prompt("What is your current Longitude?"));
var intDestinationLatitude = parseFloat(prompt("What is your destination Latitude?"));
var intDestinationLongitude = parseFloat(prompt("What is your destination longitude?"));

if ( (intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
output.textContent = "We'd be headed North East, capt'n!";
}
else if ( ( intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
    output.textContent = "Ye'd best head North West, captain!";
}
else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
output.textContent = "Ye'd best head South West, captain!";
}
else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
output.textContent = "Ye'd best head South East, captain!";
}
else{
output.textContent = "Land Ho!";
}
<head>
<meta charset="utf-8">
  <title>More if</title>

<body>
 <div id= "output">


 <div id= "input2">
    </div>

</body>


<script src="moreif.js"></script>
<script src="moreif2.js"></script>
</head>
</html>
michaelmesser
  • 3,601
  • 2
  • 19
  • 42