title: Writing first 100 prime numbers to a file using node.js
body: <p>I am trying to teach myself node.js (no javascript or real programming experience) and have hit a road block on one of the problems I am trying to solve. My goal is to write the first 100 prime numbers to txt file. Below is my code so far.</p>
  
  <pre><code>var fs = require('fs');
  var outfile = "test.txt";
  var primality = function () {
      var arr = [];
      for (var n = 2; n &lt;= 542; n++) {
          var primeTrue = true;
          for (var i = 2; i &lt;= Math.sqrt(n); i++) {
              if (n % i === 0) {
                  primeTrue = false;
              }
          }
          if (primeTrue) {
              arr.push(n);
          }
      }
      return arr;
  }
  fs.writeFileSync(outfile, arr);
  </code></pre>
  
  <p>I was using the codecademy javascript lab to test my loop and conditions and this code does seem to work. (It is also likely not the best way to do this since I had to set my counter to stop at 542 rather than have the program stop at the 100th prime number). In any event, when I added</p>
  
  <pre><code>var outfile = "test.txt"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>fs.writeFileSync(outfile, arr);
  </code></pre>
  
  <p>this did not output the 100 prime numbers to the txt file as I thought it would. I'm still on the ground floor of my learning so I greatly appreciate any help you can provide. </p>
  
  <p>Thank you in advance.</p>
  
  <p>Kevin</p>