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?
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