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"
See below the code I applied:
everything works fine but it doesn’t allow me to continue if I run the code.
and also what it should return if I pass the same year of my year of birth (in this case 1992)?
console.log(howOld(1992, 32));
You were 0 in the year 1992
Looks to be what one would expect.
What error message is the LE giving you as to why your code is not being passed?
Aside
let currentYear = 2024;
The above is meant as a global constant so better declared with, const
. However, it is arbitrary as well, and will require editing in three months.
We can make the code durable and permanently usable by polling the system, via the Date()
constructor, for the current year.
const today = new Date();
const currentYear = today.getFullYear()
Also, consider writing these lines inside the function so they are not taking up memory in the global namespace. Functions give back their memory when exited.
thank you so much for your reply. really appreciate the effort and time you took to answer this matter. the LE is showing the following message:
“If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]”
Regarding the aside: thank you I will make treasure of your suggestions! these tips are really eye opening!
You’re welcome! Not surprised but pleased to see some eyes opened. Means we recognize value when we see it (no thanks to me).
The error message makes no sense to me given it checked out for 2028. I’d like to say set the above code aside and start from scratch, rethinking your logic in the inverse to really loosen the grey matter. Expect the same results in terms of the returns being correct for all test cases.
Unfortunately that might take up a lot of your time, and can wait until you loop back for review in the coming weeks. It is not a sin to view the recommended solution, with a careful eye, and look for every possible clue as to why your code won’t pass, but the solution code will. You don’t have to accept the solution code if you can fix your own and still pass.
Relating to the Aside: One failed to mention the other benefit to setting constants inside a function… It is timely, and fresh on every call to the function, regardless how long the current session has been in effect. I leave browser tabs open for days, which means they are running on old constants.
Bottom line, declare as few global constants as possible, and be sure they are truly constant and unchanging, similar in nature to PI or E, et al. Start your functions with up to date values and every run of the function will be fresh, and the return can be more easily trusted as reliable. And, it is one less thing to fix later. If the function is failing any test cases, you won’t be able to blame it on the constants you define within that scope.
Happy coding!
Looking at the other solutions in the thread, age
is supposed to be the first parameter and year
is supposed to be the second parameter.
Your solution has swapped the two parameters.