I just went through my code, it's this one damn line that's causing my main suffering (I'm running the js through
https://onecompiler.com)
This one:
age = parseInt( prompt("What is your age in years: ") );
If I change the age to any other number, then the calculator works. But otherwise, that line causes the try..catch to go off and it gives me a NaN answer
Is there any other way of getting input through the command line? Sort of like input() in Python..?
You can't use prompt there.
Rather save this to a .html file, run that file.
JavaScript:
<!DOCTYPE html>
<html>
<body>
<script>
// calculate the user's age in seconds
function calculate_age(age_year) {
// get the months from the years
var months, days, hours, minutes;
months = age_year * 12;
// 30 days were used as a common denominator for each month
days = months * 30;
//get the hours from the days
hours = days * 24;
//get the minutes from the hours
minutes = hours * 60;
//get the seconds from the hours
var seconds = minutes * 60;
return seconds;
};
// get the user's age in years
function get_age() {
var age;
try {
age = parseInt( prompt("What is your age in years: ") );
} catch (TypeError) {
console.log("Invalid input entered");
}
return age;
};
// output the user's age in seconds
console.log("Age in seconds: " + calculate_age(get_age()));
</script>
</body>
</html>

Entering 20:
I'd have made it an alert probably. And as said, learn to change that to a form, etc., but you're still starting out so all good.
My suggestion is to never use parseInt(prompt()); in one step, you should write code so that it does one thing at a time, it can become very difficult if you have too much happening on the same line, e.g. here you're displaying a prompt, getting a result, and converting that result to an int, move the step of conversion to a new line, you could maybe want to do extra checks etc. to make sure it's a number (if checks).
Use something like VS Code to edit so you get code highlighting:
Visual Studio Code is a free, open source AI code editor. Build with AI agents that plan, code, and debug for you. Manage multi-agent workflows across environments on Linux, macOS, and Windows.
code.visualstudio.com
Extension if you are interested that are good:
Emmet (
built-in, hit tab on suggestions)
bracket pair colorizer 2
Prettier
Gitlens
Eslint
auto rename
auto close
material theme
In terms of your code, next steps:
- make it into a form with a submit button
- handle bad input (e.g. text (syntax since parseInt), negative numbers(semantic), unrealistic numbers like 3000 (semantic))
- change to calculate by year then day, calculate leap years
- change after to using date (you should use built-in, then
date-fns and understand what the difference is/why you'd want date-fns)
Once you run directly in browser, you can use let, const and var.
Var = global variable if defined outside of a function, if declared within a function block, it's available in the entire function.
Let = only within the scope it is declared, if e.g. in for loop, it will only exist within that loop, by scope I mean {}.
Const = constant, you're not assigning a different value after declaration, only exists withing scope.
Good read:
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/