I feel like I’ve made my code unnecessarily complicated. Thoughts on how to make this more concise and legible?
The Codebyte doesn’t run; but, we did copy your code to the console and ran it through all the possible scenarios with bona fide results on all counts. Good job making your code keep its promises.
When we get down to fairly simple logical structures there is not enough to be critical of, so don’t be disappointed if we don’t criticize your code. That would involve analysis that would, frankly, be wasted.
Are there other ways to tackle this problem? Likely. At this stage, would it be a holy grail? No. The only reason to review this code is with the aim to discover another way to do the same thing. That is endemic in code research. We always want to know if it can be done differently. Science is about questions, not answers.
As for the code, proper, the only criticism I have is on the comment in line 3. We don’t ‘calculate’ the current year. We fetch it from a Date
object.
Thanks so much for your feedback on the line 3 comment and for taking the time to write such a detailed response! Just beginning with JS and I’m starting to better understand how objects fit into functions.
// Write your function here:
// delcare a variable for the year that individual was born
const howOld = (age, year) => {
const birthYear = 1994;
if (birthYear < year) {
age = year - birthYear;
return `You will be ${age} in the year ${year}`;
} else if (birthYear > year) {
age = birthYear - year;
return `The year ${year} was ${age} years before you were born`;
} else if (birthYear < year || year >= birthYear) {
age = year - birthYear;
return `You were ${age} in the year ${year}`
}
};
// Once your function is written, write function calls to test your code!
console.log(howOld(0, 1995));
I’m confused because when I call the howOld function with different years everything works but wont let me
pass I know my way is different and I’d love any and all helpful criticism on how I could improve my code. Please and thank you in advance.
Ps: How exactly should I be using the age parameter when calling the function?
Birth year should be computed in the function given the ‘age’ parameter and the current year which we can poll using the datetime class.
from datetime import date
current_year = date.today().year
birthyear = current_year - age
Aside
It is rarely a good idea to hard code literal constants. It makes our code rigid and arbitrary. Just saying…
Thank you kindly I’ll keep that in mind for the future, also I appreciate the help.
Code works, but probably could have defined the birthyear, not just make up the formula each time.
// Write your function here:
const howOld = (age, year) => {
if (year > 2024) {
return `You will be ${((year - 2024) + age)} in the year ${year}.`;
} else if (year < (2024 - age)) {
return `The year ${year} was ${((2024 - age) - year)} years before you were born.`;
} else if (year >= (2024 - age)) {
return `You were ${(year - (2024 - age))} in the year ${year}`;
}
};
// Once your function is written, write function calls to test your code!
console.log(howOld(29, 2050)); // testing the future age. Expected output: "You will be 55 in the year 2050."
console.log(howOld(29, 1969)); // testing year before birth. Expected output: "The year 1969 was 26 years before you were born."
console.log(howOld(29, 2003)); // testing age after birth, but before today. Expected output: "You were 8 in the year 2003"