Those are steps in the right direction. But take into account some things:
You should have the variables in the scope of the function (right now you are defining the variables globally)
The method getFullYear() is part of Date. Considering that you are doing let day = new Date() then you'd need to do day.getFullYear() to get the current year.
In the console.log, you should return the result of the function and not the value that you pass to it.
With those changes, the code would look like this:
One of the things that you should do too is having descriptive names for variables. Right now you are storing dates in day, the year in day1, the birth year in n... That will be helpful in the future to maintain the code and help debug it.
The variables will only work in the function/block where they were defined. So if you want to use it somewhere else, you'll need to redefine it or make it global.
let day = new Date();
let birthday = day.getFullYear;
function birh(n){
let calculate = day.getFullYear - n;
return(calculate)
}
console.log(n);
something like this?
Those are steps in the right direction. But take into account some things:
getFullYear()
is part ofDate
. Considering that you are doinglet day = new Date()
then you'd need to doday.getFullYear()
to get the current year.console.log
, you should return the result of the function and not the value that you pass to it.With those changes, the code would look like this:
One of the things that you should do too is having descriptive names for variables. Right now you are storing dates in
day
, the year inday1
, the birth year inn
... That will be helpful in the future to maintain the code and help debug it.Understood, in case if u want to call in another function day.getFullYear must make new, cuz this is work only in block of function birth, right?
Thank you for advice, I must try do give more descriptive names for var.
The variables will only work in the function/block where they were defined. So if you want to use it somewhere else, you'll need to redefine it or make it global.
I get it, thank you for the explanation you help me a lot.
When I get a job, from my first paycheck I will buy you a coffee.
thank you one more time. Cheers!