qTips
qTips

Reputation: 23

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. 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());
  }

Upvotes: 0

Views: 78

Answers (1)

Sebastian Richner
Sebastian Richner

Reputation: 760

You defined the Map and the method toDigits, but are not actually calling the method. You can do that by adding toDigits(...). To parse the command line arguments you can use process.argv. This will give you something like

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

Which you can use e.g., with process.argv.slice(2)in your code.

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);

Upvotes: 1

Related Questions