Home > Software engineering >  ReferenceError: readline is not defined, can someone help me to understand how the readline works
ReferenceError: readline is not defined, can someone help me to understand how the readline works

Time:09-03

let n = parseInt(readline());
sumAndAverage(n);

function sumAndAverage(n) {
    var sum = 0;
    for (let i = 1; i <= n; i  ) {
        sum  = i;
    }
    console.log(sum);
    console.log(Math.floor(sum / n));
}

CodePudding user response:

Readline is a module, not a global. You have to import or require it (depending on if you are writing an EJS or CJS module).

CodePudding user response:

var readline = require('readline');
          
var rl = readline.createInterface(
         process.stdin, process.stdout);

    
rl.question('Enter the number: ', (num) => {
    if(typeof num === 'string'){
        let int = parseInt(num)
        sumAndAverage(int)
    }else if (typeof num === 'number'){
        sumAndAverage(num)
    } else return console.log('Please enter the number.')
    rl.close();
});

function sumAndAverage(n) {
    var sum = 0;
    for (let i = 1; i <= n; i  ) {
        sum  = i;
    }
    console.log(sum);
    console.log(Math.floor(sum / n));
}
  • Related