howOld() - Issue

// Write your function here:

function howOld(age,year){
const today = new Date();
const tyear = today.getFullYear();
var diff = tyear - year;
var aged = tyear - year + age;

if(age < aged){
return You will be ${aged} in the year ${year};
}else if(aged < 0){
returnThe year ${year} was ${diff} years before you were born;
}
else{
return You were ${aged} in the year ${year};
}
}

// Once your function is written, write function calls to test your code!
console.log(howOld(18,2023));

why if my first If statement being ignore?

1 Like

How do you mean it’s getting ignored? Can you post the result as well, please?

Input age for 8 and year 2023

I assume the age that i input will be compare to aged 2021 - 2023 + age then output the return You will be ${aged} in the year ${year} but it does not and goes straight to return You were ${aged} in the year ${year} ;

If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’

OK I check the solution and still keep getting errors like this. Instead of copying and paste I try to type the solution so I can both see and do the solution. And one of the big difference I did was that the past check is in the middle of the if-else function. And that is it.

This section of the Javascript fundamental is messing with me this past week

// Write your function here:

function howOld(age,year){
const today = new Date();
const tyear = today.getFullYear(); //today’s year
var diff = year - tyear; //year - 2021
var Nage = age + diff; //age + year - 2021

if(Nage < diff){
return You will be ${Nage} in the year ${year};
}else if(Nage < 0){
returnThe year ${year} was ${-diff} years before you were born;
}
else{
return You were ${Nage} in the year ${year};
}
}

// Once your function is written, write function calls to test your code!
console.log(howOld(4,2040));

const howOld = (age,year) => {
let today = new Date();
let tyear = today.getFullYear(); //today’s year
const diff = year - tyear; //year - 2021
const Nage = age + diff; //age + year - 2021

if (Nage < 0) {
    return `The year ${year} was ${-Nage} years before you were born`
} else if (Nage > age) {
    return `You will be ${Nage} in the year ${year}`
} else {
    return `You were ${Nage} in the year ${year}`

}};

Ok this is the solution but i don’t know why

I presume you’re stuck with the variables?
diff calculates the difference between the inputted year and the current year.
Nage calculates the new age: your current age plus the difference of the two years; so how old you’ll be in year.
Does this help?

Yes thank you :wave:

1 Like