Moniruzzaman Sujon
Moniruzzaman Sujon

Reputation: 568

Take multiple inputs from user using readline in Node. Where do I call the function?

I'm a complete beginner. So, here on Stack Overflow I found this solution of "How to take multiple input using readline, but I can't find how to apply this the function. I put it in the main function and it takes input from me but it's not working. My output is showing NaN. Can anyone help me to fix this?

Here is my code

function simpleInterest (money, rate, time) {
    simpleInterest=money*rate*time;
    return simpleInterest;
}

let money;
let rate;
let time;

'use strict'

const readline = require('readline')

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

const question1 = () => {
  return new Promise((resolve, reject) => {
    rl.question('Total Money: ', (money) => {

      resolve()
    })
  })
}

const question2 = () => {
  return new Promise((resolve, reject) => {
    rl.question('Interest Rate: ', (rate) => {

      resolve()
    })
  })
}
const question3 = () => {
  return new Promise((resolve, reject) => {
    rl.question( 'How Many Years: ', (time) => {
   
      resolve()
    })
  })
}

const main = async () => {
  await question1()
  await question2()
  await question3()
  //Here I'm calling the Function
  let profit= simpleInterest(money, rate, time)
    console.log("total", profit)
    rl.close()
}

main()

Upvotes: 1

Views: 1136

Answers (1)

ggorlen
ggorlen

Reputation: 57175

You're missing a couple of key pieces to the promises puzzle. resolve() accepts an optional argument, which is the value the promise should resolve to. Without an argument, the promise resolves to undefined. Synatically, this looks like resolve(money), for example.

The second step is assigning the value of the resolved promise to a variable or using it in an expression. const money = await question1(), for example.

It's possible to work around these steps by assigning to the global variables you've created from the callbacks, but I prefer to use promises which keeps things locally scoped.

Another nuance that's easy to miss is that user input is returned as a string. You can convert these strings to numbers with +.

As an optimization, you have virtually the same code three times with different variable names. This indicates a potential opportunity to abstract the logic to a function that you can call with different parameters, avoiding repetition.

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const ask = msg => new Promise(resolve => 
  rl.question(msg, response => resolve(response))
);

const simpleInterest = (money, rate, time) =>
  money * rate * time
;

const main = async () => {
  const money = await ask("Total Money: ");
  const rate = await ask("Interest Rate: ");
  const time = await ask("How Many Years: ");
  console.log(simpleInterest(+money, +rate, +time));
  rl.close();
};

main();

Now, error handling hasn't been added, so you might want to write a loop to keep prompting until the user has entered a number.

Upvotes: 2

Related Questions