Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

334
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda