Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!