0

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());
  }
qTips
  • 13
  • 1
  • 7

1 Answers1

1

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);
Sebastian Richner
  • 782
  • 1
  • 13
  • 21
  • This doesn't answer the question? _"when I run my `node file.js 1 23 44` script, it doesn't print out anything"_ – evolutionxbox Apr 28 '21 at 20:21
  • You're right. Updated the answer with a short example on how to parse the command line arguments. – Sebastian Richner Apr 28 '21 at 20:29
  • Thanks for updating the answer. – evolutionxbox Apr 28 '21 at 20:31
  • I think this might be the solution @SebastianRichner, @evolutionxbox. I used the `process.argv.slice(2)` before but from what I know the input are strings and not integers, so now my question is, are the arugments in `node file.js 1 23 44`, still integers? – qTips Apr 28 '21 at 20:36
  • @qTips they're strings. Do you need them to be numbers? Looking at this answer I don't think you do – evolutionxbox Apr 28 '21 at 20:37
  • They're strings. So you can safely remove the `n.toString(10)` line. (Or leave it, as string.toString() will still simply return a string.) – Sebastian Richner Apr 28 '21 at 20:38
  • @SebastianRichner @evolutionxbox, my problem is am supposed to convert an array of integers into an array of strings representing the phonetic equivalent of the integer. This is why am using the `toString()` function. – qTips Apr 28 '21 at 20:42
  • Using the command line `node file.js 1 23 44` the "numbers" are strings. So there's no need to convert them. – evolutionxbox Apr 28 '21 at 20:43
  • the only thing that worries me is that input needs to be integers, because if they are strings already than am not technically converting the array of integers into an array of strings. @evolutionxbox – qTips Apr 28 '21 at 20:56
  • What is the question that's being asked? Does it specify a command line input? – evolutionxbox Apr 28 '21 at 20:58
  • `Convert an array of integers into an array of strings representing the phonetic equivalent of the integer.` this is the question, @evolutionxbox – qTips Apr 28 '21 at 20:59
  • `Given an array: [3, 25, 209], print “Three,TwoFive,TwoZeroNine” into stdout.` this is the example they gave me @evolutionxbox – qTips Apr 28 '21 at 21:00
  • Command line arguments will always be passed as strings. The question does not specify that the integers need to come from the command line. @qTips can you specify what is missing in the answer? – Sebastian Richner Apr 28 '21 at 21:01
  • @SebastianRichner, the only thing I know is that they will run my code like this `node nameOfFile.js 1 33 400`. Thats why am so confuse about the question because I don't know where the array of integers are coming from. – qTips Apr 28 '21 at 21:05
  • If that is the case, then the code from the answer will print the expected output and you can remove the `toString()` part. – Sebastian Richner Apr 28 '21 at 21:09
  • @SebastianRichner, how do you interpret this question, or what would you go about this? – qTips Apr 28 '21 at 21:13
  • 1
    If they will run your code like you've mentioned, then you can safely assume that this does what is asked in the question, since it does print the words as expected. – Sebastian Richner Apr 28 '21 at 21:17
  • @qTips, Please consider accepting this answer, if it was helpful. – Sebastian Richner Apr 29 '21 at 13:47