简体   繁体   English

当我运行我的 `node file.js 1 23 44` 脚本时,它不会打印任何东西

[英]when I run my `node file.js 1 23 44` script, it doesn't prinout anything

When I run node file.js 1 23 45 , it's supposed to printout One TwoThree FourFive but for some reason it's not printing out.当我运行node file.js 1 23 45时,它应该打印出One TwoThree FourFive但由于某种原因它没有打印出来。 I think the script runs fine because I get no issues running it but it just doesn't printout anything.我认为脚本运行良好,因为我运行它没有问题,但它只是没有打印出任何东西。 Am I missing something or am I completely wrong?我错过了什么还是我完全错了?

 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 defined the Map and the method toDigits , but are not actually calling the method.您定义了 Map 和方法toDigits ,但实际上并未调用该方法。 You can do that by adding toDigits(...) .您可以通过添加toDigits(...)来做到这一点。 To parse the command line arguments you can use process.argv .要解析命令行 arguments 您可以使用process.argv This will give you something like这会给你类似的东西

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

Which you can use eg, with process.argv.slice(2) in your code.您可以在代码中使用例如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);

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

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