FAQ: Code Challenges: JavaScript Fundamentals - howOld()


#1

This community-built FAQ covers the “howOld()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise howOld()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I’m not really sure why I’m getting this error message when I try to submit this exercise. It’s literally saying exactly what it needs to.

Can anyone point out what I may have done wrong?


#3

Perhaps try logging the return value at the caller, not in the function. Eg.

return `You will be ${newAge} in the year ${year}`;

#4

Not sure what the the program has a problem with, anyone knows what the problem is? I mean I think that’s what i wrote down…


#5

Compare your code to the following…

const howOld = (age, year) => {
  var currentYear = new Date().getFullYear();
  var birthYear = currentYear - age;
  var diff = Math.abs(birthYear - year);
  if (currentYear - year < 0) {
    return `You will be ${diff} in the year ${year}`;
  }
  if (year < birthYear) {
    return `The year ${year} was ${diff} years before you were born`;
  }
  return `You were ${diff} in the year ${year}`;
};

I’ve done the arithmetic at the start and just carried the variables through the logic. Can’t say for sure why your code didn’t pass.

Might I suggest copying your code and pasting it elsewhere, then get the solution and copy it so you can compare the two. Fix your code and paste it back in, then Run.


#6

Thank you very much!


#7

some shшt-code (as i think) and function is ready


#8

This works perfectly but is not being acknowledged as correct?


#9

nvm, looks like the problem was not using exact wording provided in instructions for return - changed it thus and now all works


#10

You guys are too smart


#11

@MTF , your code is much more elegant than what I wrote. Can you explain why codecademy stays away from teaching us “var” to declare variables? Everything I have seen so far in the course is “let” or “const”.


#12

With the introduction of let and const in ES6 it would seem we no longer need var but to my mind it should be based on use case, not imperative. var and let are almost the same with one major difference. var does not define block scope in a loop body, whereas let does.

One supposes that the author is trying to get away from var entirely, and I cannot doubt many teachers are of the same mind. The one main reason for it not being deprected from the language would be backward compatibility.

In global scope, var declarations are rather moot. All variables declared without var, regardless what scope they may be declared in, will be global by default. That’s how JS treats them.

function foo() {
    bar = "I'm in global scope"
}
console.log(bar);    // I'm in global scope

Essentially, the only real declaration keyword needed in global scope is const, which is intended to preserve values or structures. let and var are, as stated, moot.

In function scope, declared variables using a keyword all take the scope in which they are declared. It won’t always be the case where we need block scope on our loops, but when we do, let is how we do that.

for (let i = 0; i < 10; i++) {
  //
}
console.log(i)
// <- Uncaught ReferenceError: i is not defined

Notice that i is not defined outside of the for block.

for (var i = 0; i < 10; i++) {
  //
}
console.log(i)
// <- 10

Sometimes we might want the variable to be accessible outside of the loop, so the above is valid. A let declaration before the loop would have the same effect of keeping the variable accessible after the loop.

let i;
for (i = 0; i < 10; i++) {
  //
}
console.log(i)
// <- 10

For the most part, we no longer need var in the purest sense, so there is no real requirement to include it in our lexicon of ES6, just so long as we understand the difference.


#13

@MTF, This is an outstanding response! Thank you for the quick reply and the information. To be honest, I am not far enough along the course to completely understand what you are saying but I do get the gist. Thank you.


#14

take a look at this :

const howOld = (age , year) => {

if (year > 2019) {
return You will be ${age + year - 2019} in the year ${year} }
else if (year < 2019 && age >= 2019 - year) {
return You were ${age + year - 2019} in the year ${year}
}
else if (year < 2019 && age < 2019 - year) {
return The year ${year} was ${2019 - age - year} years before you were born
}
}

console.log(howOld(44 , 3321))


and now take a look at this :

const howOld = (age , year) => {

if (year > 2019) { return
You will be ${age + year - 2019} in the year ${year} }
else if (year < 2019 && age >= 2019 - year) {
return You were ${age + year - 2019} in the year ${year}
}
else if (year < 2019 && age < 2019 - year) {
return The year ${year} was ${2019 - age - year} years before you were born
}
}

console.log(howOld(44 , 3321))


the first one prints what we want,
but the second one prints : undefined

I couldn’t realize what’s wrong with my code at first because I didn’t know the ‘difference’ would make such a change.
anyone who figures it out gets a cookie.