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

333
Visualizações
How can I get my prompt(); to call my function AND give it an argument?

I'm doing a challenge that had me make a days/weeks/month calculator until you're 90.

I finished it but I can only call the function by calling it in the console like this lifeInWeeks(25); I used 25 since that's my age.

But I want to know how can I do this and have a prompt pop up and ask what their age is and then give an alert with the answer, but for this to happen the prompt would have to call the function and give it input from the answer i.e. the prompt should call the function and the answer of that prompt should be the function's argument.

Something like this:

"What is your age?" (you answer) 25

Then an alert pops up and says:

"You have 23725 days, 3380 weeks, and 780 months left."

WITHOUT having to manually call the function in the console like this: lifeInWeeks(25);

Here's my code:

function lifeInWeeks(age) {
    var days = (90 - age) * 365;
    var weeks = (90 - age) * 52;
    var months = (90 - age) * 12;
    alert("You have " + days + " days, " + weeks + " weeks, and " + months + " months left.");

}

lifeInWeeks(25);

I've tried putting the prompt inside the function but that hasn't worked, any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can simply put prompt user for entering age, and then use then value from user input

let life = prompt("What is your age ?")
lifeInWeeks(+life);

function lifeInWeeks(age) {
    var days = (90 - age) * 365;
    var weeks = (90 - age) * 52;
    var months = (90 - age) * 12;
    alert("You have " + days + " days, " + weeks + " weeks, and " + months + " months left.");

}

You can also move this inside function, in that case you need to invoke the function without any parameter

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