-1

我遇到了以下错误,我不太确定为什么。请记住,我正在使用 Node.js。目标是将前 100 个素数输出到“hello.txt”文件。请告知如何让这个工作!谢谢。

错误信息:

return arr.join(“,”);
^
SyntaxError: Unexpected token ILLEGAL
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

前 100 个素数代码:

#!/usr/bin/env node

var prime = function (n) {
if (n === 2 || n === 3 || n === 5 || n === 7 || n === 11 || n === 13 || n === 17 || n === 19) {return n;}
if (n % 2 > 0 && n % 3 > 0 && n % 5 > 0 && n % 7 > 0 && n % 11 > 0 && n % 13 > 0 && n % 17 > 0 && n % 19 > 0 && n % Math.sqrt (n) > 0) {return n;}
};

var first100primes = function(k) {
var n = 1;
var arr = [];

for (n = 1; n < k+1; n++) {
arr.push(prime (n));
}
arr = arr.filter(Number);
arr.length=100;
return arr;
};

var fmt = function (arr) {
return arr.join(“,”);
};

var final = (fmt(first100primes(1000)));

var fs = require(‘fs’);
var outfile = “prime.txt”;
var out = final + “,”;
fs.writeFileSync(outfile, out);
console.log(“Script: ” + __filename + “\nWrote: ” + out + “To: ” + outfile);
4

1 回答 1

3
于 2013-07-03T13:35:38.027 回答