简体   繁体   English

意外令牌ILLEGAL(前100个素数)

[英]Unexpected token ILLEGAL(first 100 primes)

I'm running into the below errors and I'm not quite sure as to why. 我遇到以下错误,但我不确定为什么。 Bare in mind, I'm using Node.js. 简而言之,我正在使用Node.js。 The goal is to output the first 100 prime numbers to a 'hello.txt' file. 目标是将前100个质数输出到“ hello.txt”文件。 Please advise how to get this working! 请告知如何使它工作! Thanks. 谢谢。

error msg: 错误消息:

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

first 100 primes code: 前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);

is not the same as " . Similarly, ' and ' are not the same. " 。类似地, ''也不相同。

Change each occurance of and ' to " and ' respectively, and your code should work fine. 将每次出现的'分别更改为"' ,您的代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM