I am writing this out of self-awe.
After a month struggling with the essentials of JS functions, and feeling freakin’ frustrated about 'em… after weeks of repeating the same exercises to make sure that I truly understood how things worked… I encounter this Code Challenges: JavaScript Fundamentals practice and I start with it, exercise by exercise…
I resolve the first few of them in just a few minutes… and although I feel good about it, I can’t help some sense of being solving something like basic arithmetic, so to speak. It is when I get to the HowOld() exercise and I frown at it massively. It feels a bit more difficult of all the previous challenges (a lot, actually)… but, hey, I’ve been able to do all the other ones without having to consult a single hint or having to take a look at my notes so… I start facing it.
Then, it comes kind of clear to me. I feel enlightened or something. Like I doubt not, not at all, and I just type and type and type…
All of a sudden, this appears written in front of my eyes:
howOld?
function howOld(age, year) {
const currentYear = 2020;
const birthYear = currentYear - age;
if (year === currentYear) {
return `You are ${age} in ${currentYear}.`;
} else if (year > currentYear) {
let futureYear = year - currentYear;
let futureAge = age + futureYear;
return `You will be ${futureAge} in the year ${year}.`;
} else if (year < birthYear) {
let priorYear = birthYear - year;
return `The year ${year} was ${priorYear} years before you were born.`;
} else if (year > birthYear && year < currentYear) {
let calculatedAge = year - birthYear;
return `You were ${calculatedAge} in the year ${year}.`;
} else if (year = birthYear) {
return 'This was the year you were born in. Thus, you were 0.';
} else {
return 'Hmm... You got me there!';
}
}
And then I start trying it out logging parameters (is that the right term?)
console.log(howOld(35,1980));
console.log(howOld(35,1985));
console.log(howOld(35,1990));
console.log(howOld(35,2020));
console.log(howOld(35,2030));
And I surprisingly get 5 answers with no errors, such as:
The year 1980 was 5 years before you were born.
This was the year you were born in. Thus, you were 0.
You were 5 in the year 1990.
You are 35 in 2020.
You will be 45 in the year 2030.
I can’t believe it. At that point, I am kinda dancing around my laptop
Then I try:
console.log(howOld(35,'whatever'));
and I get:
This was the year you were born in. Thus, you were 0.
So I know that there’s still something to be improved… but I still can’t believe that I sorta made it… ‘coz I sorta made it, didnt’ I? Ot am I just being way too optimistic?
Now, besides the necessary validation of a number to be passed as a parameter (hope to be getting the vocabulary right), is it really some kinda good piece of code? Because it doesn’t seem too long, and I’ve somehow understood that one good practice about coding is to keep it as short as possible… is that right?
And much more important… which ways — besides validation — are there for that code be improved?
Thanks in advance for all the feedback you coders can provide me with
Cheers!
~Ibai