Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
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()
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda