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
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?
You are trying to call a function 'fmt' which you haven't defined.
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.