0

I'm writing a node.js script that writes the first 100 prime numbers to a file, with each prime separated by a comma.

#!/usr/bin/env node
function listPrimes( max ) {
    var primes = [];
    var sieve = new Array( max );
    for( var i = 0;  i < max;  i++ ) {
        sieve[i] = true;
    }
    for( var p = 2;  p < max;  p++ ) {
        if( sieve[p] ) {
            // p is prime, save it and mark p*2, p*3, etc. as non-prime
            primes.push( p );
            for( var t = p * 2;  t < max;  t += p ) {
                sieve[t] = false;
            }
        }
    }
    return primes;
}

var k = 20;
console.log("listPrimes(" + k + ")");
console.log(fmt(listPrimes(k)));

When I try to put it through I get this error:

ubuntu@ip-172-31-47-235:~$ node hw2.js
listPrimes(20)

/home/ubuntu/hw2.js:22
console.log(fmt(listPrimes(k)));
            ^
ReferenceError: fmt is not defined
    at Object.<anonymous> (/home/ubuntu/hw2.js:22:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

I have no idea what's wrong, I'm a beginner so it may be very obvious.

Edit: Fixed

#!/usr/bin/env node
function listPrimes( max ) {
    var primes = [];
    var sieve = new Array( max );
    for( var i = 0;  i < max;  i++ ) {
        sieve[i] = true;
    }
    for( var p = 2;  p < max;  p++ ) {
        if( sieve[p] ) {
            // p is prime, save it and mark p*2, p*3, etc. as non-prime
            primes.push( p );
            for( var t = p * 2;  t < max;  t += p ) {
                sieve[t] = false;
            }
        }
    }
    return primes;
}

var k = 100;
console.log("listPrimes(" + k + "):" + listPrimes(k));

Returns:

ubuntu@ip-172-31-47-235:~$ node hw2.js
listPrimes(100):2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
Darrin Crow
  • 43
  • 1
  • 5

3 Answers3

1

Change the two lines

console.log("listPrimes(" + k + ")");
console.log(fmt(listPrimes(k)));

to the single line

console.log("listPrimes(" + k + "): " + listPrimes(k).join(", "));

You don't need fmt and anyway that's not how you would use it...

edited just realized that listPrimes returns an array, not a single value. Hence added the join - it allows me to put "comma, space" between the numbers which looks a bit nicer than all the digits crammed together.

As for your "afterquestion" - not sure what you are doing wrong. The following is what happens on my machine (having copied your file hw2.js to a local directory):

bigMac:node floris$ node hw2.js 
listPrimes(100): 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
bigMac:node floris$ node hw2.js > output.txt
bigMac:node floris$ cat output.txt
listPrimes(100): 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
bigMac:node floris$ dir
total 16
drwxr-xr-x    4 floris  floris   136 Jul  4 16:17 .
drwxr-xr-x  140 floris  floris  4760 Jul  4 16:13 ..
-rw-r--r--    1 floris  floris   561 Jul  4 16:15 hw2.js
-rw-r--r--    1 floris  floris   112 Jul  4 16:17 output.txt
bigMac:node floris$ 

As you can see, when I run the command with the > output.txt, the output is directed to a file. Given the error you are getting, you must be doing something different - I can't figure out from your comment what it is. Did you put the > in the source file, perhaps?

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Thanks, I think that fixed it, how would I make it export the prime numbers to a text file? I thought this would accomplish that but I'm getting nothing. – Darrin Crow Jul 04 '13 at 16:17
  • This exports "to console". Depending on your operating system, if you run your program with a `> filename` at the end, it will usually send the output to this file. – Floris Jul 04 '13 at 16:27
  • I'm using ubuntu I added '> filename' to the end and got this got this error. Do I have to create the file first or? `ubuntu@ip-172-31-47-235:~$ node hw2.js /home/ubuntu/hw2.js:22 > hw2.text ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3` – Darrin Crow Jul 04 '13 at 18:08
  • Can you give the exact command you typed? It looks like the error occurred during compilation rather than when you were running it. – Floris Jul 04 '13 at 19:22
  • ubuntu@ip-172-31-47-235:~$ node hw2.js – Darrin Crow Jul 04 '13 at 22:20
  • See my code edit. The `> output.txt` is part of the command, not part of the code itself. So - use the code you had (that worked), and invoke it with `node hw2.js > output.txt` . Does that clarify it for you? – Floris Jul 04 '13 at 22:23
  • Got it, and if I wanted my program to generate the text file how would I do that? – Darrin Crow Jul 04 '13 at 23:11
  • You will find exactly what you need by looking at the answers to [this question](http://stackoverflow.com/questions/2496710/nodejs-write-to-file). Let me know if that doesn't solve it for you. I think the third answer is best for your needs (using `stream.write`) – Floris Jul 05 '13 at 00:08
0

You are trying to call a function 'fmt' which you haven't defined.

Ronnie
  • 1,059
  • 14
  • 27
0

As others have suggested, there is no function fmt.

Since you are returning an array, your last line may as well look like:

console.log(listPrimes(k).join(","));

Yes, there is no sanity check on the return value of listPrimes() as it always returns something.

sjngm
  • 12,423
  • 14
  • 84
  • 114