Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Help needed to read command line arguments in Nodejs

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 233
    Answer it

    Hi,

    I am trying to accept input from the command line in Nodejs using process.argv in the code snippet, but currently getting an error when I run the following command on the terminal - node index.js "1 18"

    Error that I get - TypeError: myCallback1 is not a function
     

    Could you please help me in resolving this issue?

    This line of code will help to read command line arguments
    let arguments = process.argv; 
    
    function findPrime(string, myCallback1, myCallback2, myCallback3) {
      let str = string.split(' ');
      console.log(str);
      if (+str[0] < 0 || +str[1] < 0) {
        return 'Please provide a positive integer as negative integers cannot be prime';
      }
      if (+str[1] <= +str[0]) {
        return'Input range incorrect';
      }  
      return myCallback1(str, myCallback2, myCallback3);
    }
    
    function createPrimeList(str, myCallback1, myCallback2) {
      let primeList = [];  for (let i = +str[0]; i <= +str[1]; i++) { 
        let count = 0;
        for (let j = 2; j < i; j++) {
          if (i % j === 0 ) {
            count++;
            break;
          }
        }
        if (count === 0 && i > 1) {
          primeList.push(i);
        }
      }  if (!primeList || !primeList.length) {
        return 'No prime numbers found';
      }
      if (+str[0] >= 0 && +str[1] < 11) {
        return Math.max(...primeList);
      }   return myCallback1(primeList, myCallback2);
    }
    
    function findDigitOccurrences(primeList, myCallback) {
      let digitOccurrences = primeList.join('').split('').sort();
      let count = 1;
      let obj = {};  for (let i = 0; i < digitOccurrences.length; i++) {
        if (digitOccurrences[i] === digitOccurrences[i + 1]) {
          count++;
          obj[digitOccurrences[i]] = count;
        } else {
          count = 1;
        }
      }
      return myCallback(obj);
    }
    
    const highestOccurringDigit = obj => Object.keys(obj).reduce((current, next) => obj[next] < obj[current] ? +current : +next);
    
    // The following code will help to read command line arguments   
    let primeNumber = findPrime(arguments[2], arguments[3]);
    console.log('Result:', primeNumber);

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: