0

I'm trying to learn JavaScript. I'm very new (started this week). I'm following the exercises available at javascript.info.

This is the html code I'm opening in firefox:

<p>First, we have a prompt that incorrectly concatenates input strings.</p>
<script>
  let a = prompt("First number?", 1);
  let b = prompt("Second number?", 2);

  alert(a + b); // 12
</script>
<p>Now we correctly cast to integer, and then do addition.</p>
<script>
  let a = prompt("First number?", 1);
  let b = prompt("Second number?", 2);

  a = Number(a);
  b = Number(b);

  alert(a + b); // 3
</script>
<p>Well, that's all folks!</p>

For some reason, the first script will run, but the second won't. I've checked around (https://www.quora.com/Can-I-call-two-JavaScript-files-in-one-HTML-file and Implications of multiple <script> tags in HTML) and it seems like they should just run in sequence.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Logan Schelly
  • 331
  • 2
  • 11
  • 2
    You already declared the variable. Consider checking the browser console for errors before posting – CertainPerformance May 04 '20 at 01:20
  • 1
    you could warp your code in a anonymous function. !function() { //your code here }(). This will declare the variables in function scope and solve your problem. The "!" before the functions will trigger it's execution. – Buntel May 04 '20 at 01:28

0 Answers0