0

I am trying to make a map of a mall with this script:

<!DOCTYPE html>
<html>

<head>
  <title>Display Message</title>
  <script>
    // (B) CONFIRM
    function demoA() {
      let sgl = 'Target';
      sgl;
      var destination1 = prompt("Enter Your Destination", " ");
      if (destination1 = sgl) {
        return true
        var location2 = prompt("Enter Your Closest Store", " ")
        alert("Starting Directions to " + destination1)
      } else {
        return false
      }
    }
  </script>
</head>

<body>
  <!-- (D) TEST BUTTONS -->
  <input type="button" value="Directions" onclick="demoA()" />
</body>

</html>

But, when I run this, for the destination1 var, I put Target but it doesn't ask me my closest store. Another problem is I am trying to use a string of the name of the stores but I can't get it to work.

Rana
  • 2,500
  • 2
  • 7
  • 28
  • Remove this: `sgl;` and try `if (destination1 === sgl)`. You were assigning `sql` to `destination1` instead of comparing the strings. – Andy Oct 21 '21 at 16:07
  • @SebastionSimon Can we add https://stackoverflow.com/q/14102912/215552 to the dupe list to cover the early return? – Heretic Monkey Oct 21 '21 at 17:36

3 Answers3

1

Condition should be

if (destination1 === sgl) 

and not

if (destination1 = sgl) 

Also after this line you have:

    return true

And your function done with execution there (wont reach next line)

munleashed
  • 1,677
  • 1
  • 3
  • 9
0

You are returning from the function before calling prompt a second time here:

      let sgl = 'Target';
      sgl;
      var destination1 = prompt("Enter Your Destination", " ");
      if (destination1 === sgl) {
        return true // this returns from the function and no following code is run
        var location2 = prompt("Enter Your Closest Store", " ") // never runs
        alert("Starting Directions to "+destination1) // never runs
        // put your return here to run the above
      } else {
        return false
      }
    }
Tom
  • 1,158
  • 6
  • 19
  • Thank you! It now works! – Vintheruler1 Oct 21 '21 at 16:09
  • This is a duplicate question. It has been asked before, and answered many times. Stack Overflow prefers a single source for answers to common questions. – Heretic Monkey Oct 21 '21 at 16:15
  • @HereticMonkey The linked answer only relates to part of the issue here. While I appreciate that the other aspect - returning from a function not running subsequent code - also most certainly exists already could we also link to that? – Tom Oct 21 '21 at 16:19
0

Edit: Good News! I got it to work thanks to @munleashed and @Tom! Here is the new code for those who would need it!

<!DOCTYPE html>
<html>
  <head>
    <title>Display Message</title>
    <script>
    // (B) CONFIRM
    function demoA () {
      let sgl = 'Target';
      sgl;
      var destination1 = prompt("Enter Your Destination", " ");
      if (destination1 === sgl) {
        var location2 = prompt("Enter Your Closest Store", " ") // never runs
        alert("Starting Directions to "+destination1) // never runs
        return true
        // put your return here to run the above
      } else {
        return false
      }
    }
    </script>
  </head>
  <body>
    <!-- (D) TEST BUTTONS -->
    <input type="button" value="Directions" onclick="demoA()"/>
  </body>
</html>
  • This is a duplicate question. It has been asked before, and answered many times. Stack Overflow prefers a single source for answers to common questions. – Heretic Monkey Oct 21 '21 at 16:13