-2

Working through a Javascript tutorial and got to this example that is supposed to write some temperature conversions to the screen. This is the code straight from the tutorial and it doesn't work at all. Prints nothing to the screen. I can't find any error if there is one.

<!DOCTYPE html>
<html lang="en">

  <head>
    <title>Chapter 3, Example 4</title>
  </head>

  <body>
    <script>
      var degFahren = [212, 32, -459.15];
      var degCent = [];
      var loopCounter;
      for (loopCounter = 0; loopCounter <= 2; loopCounter++) {
        degCent[loopCounter] = 5 / 9 * (degFahren[loopCounter] - 32);
      }
      for (loopCounter = 2; loopCounter >= 0; loopCounter−−) {
        document.write("Value " + loopCounter +
          " was " + degFahren[loopCounter] +
          " degrees Fahrenheit");
        document.write(" which is " + degCent[loopCounter] +
          " degrees centigrade<br />");
      }

    </script>
  </body>

</html>
Bun
  • 3,037
  • 3
  • 19
  • 29
Alfonso Giron
  • 279
  • 4
  • 11
  • Where did you get the code from? It looks like the dashes (`--`) in `loopCounter--` aren't dashes but something else. This should work: https://jsfiddle.net/g0ojv8y5/ – putvande Mar 13 '16 at 16:19
  • Beginning Javascript 5th Edition – Alfonso Giron Mar 13 '16 at 16:22
  • Oh geez lol. That's quite the bug. But yes you're right, and that did the trick. Thanks. Didn't even know that was possible. – Alfonso Giron Mar 13 '16 at 16:30
  • See: [How does “cut and paste” affect character encoding and what can go wrong?](http://stackoverflow.com/questions/1929812/how-does-cut-and-paste-affect-character-encoding-and-what-can-go-wrong) – Yogi Mar 13 '16 at 16:34

1 Answers1

2

Try this (just change to -):

<!DOCTYPE html>
<html lang="en">

  <head>
    <title>Chapter 3, Example 4</title>
  </head>

  <body>
    <script>
      var degFahren = [212, 32, -459.15];
      var degCent = [];
      var loopCounter;
      for (loopCounter = 0; loopCounter <= 2; loopCounter++) {
        degCent[loopCounter] = 5 / 9 * (degFahren[loopCounter] - 32);
      }
      for (loopCounter = 2; loopCounter >= 0; loopCounter--) {
        document.write("Value " + loopCounter +
          " was " + degFahren[loopCounter] +
          " degrees Fahrenheit");
        document.write(" which is " + degCent[loopCounter] +
          " degrees centigrade<br />");
      }

    </script>
  </body>

</html>
Stefano Nardo
  • 1,567
  • 2
  • 13
  • 23