当我运行我的"节点文件.js 1 23 44"脚本时,它不会发出任何内容



当我运行node file.js 1 23 45时,它应该打印One TwoThree FourFive,但由于某些原因,它没有打印出来。我认为这个脚本运行得很好,因为我运行它没有问题,但它只是没有打印出任何东西。我是错过了什么,还是完全错了?

const numbersMap = new Map([
[ "0", "Zero" ],
[ "1", "One"],
[ "2", "Two"],
[ "3", "Three"],
[ "4", "Four"],
[ "5", "Five"],
[ "6", "Six"],
[ "7", "Seven"],
[ "8", "Eight"],
[ "9", "Nine"]
]); 


function toDigits (integers) {
const r = [];

for (const n of integers) {
const stringified = n.toString(10);
let name = "";

for (const digit of stringified)
name += numbersMap.get(digit);
r.push(name);
}

console.log(r.join());
}

您定义了Map和方法toDigits,但实际上并没有调用该方法。您可以通过添加toDigits(...)来完成此操作。要解析命令行参数,可以使用process.argv。这会给你一些类似的东西

[
'node',
'/path/to/script/index.js',
'1',
'23',
'45'
]

例如,您可以在代码中使用process.argv.slice(2)

const numbersMap = new Map([
[ "0", "Zero" ],
[ "1", "One"],
[ "2", "Two"],
[ "3", "Three"],
[ "4", "Four"],
[ "5", "Five"],
[ "6", "Six"],
[ "7", "Seven"],
[ "8", "Eight"],
[ "9", "Nine"]
]);
function toDigits (integers) {
const r = [];
for (const n of integers) {
const stringified = n.toString(10);
let name = "";
for (const digit of stringified)
name += numbersMap.get(digit);
r.push(name);
}
console.log(r.join());
}
// You can parse command line arguments like this:
const integers = process.argv.slice(2);
// and then pass them on
toDigits(integers);

相关内容

最新更新