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.


#15

this code works fine, but it gives me error and can’t pass the exercise. Before getting the solution I want to understand what I did wrong. Thanks.

code:

function howOld(year, age){
const currentYear = 2019;
const Bornedyear = currentYear - age;
const Future = year - Bornedyear;
const Past = Bornedyear - year;
const While = year - Bornedyear;
if (year > Bornedyear && year < currentYear) {
return You were ${While} in the year ${year};
} else if (year > Bornedyear) {
return You will be ${Future} in the year ${year};
} else if (year < Bornedyear) {
return The year ${year} was ${Past} years before you were born;
} else {
return ‘error’;
}
}
console.log(howOld(2018, 19))


#16

First off, since one might assume English is not your native language, judging by the spelling, ‘Borned’ we should fix that. There is no such word in the English language.

year born => birth year

Second, we do not as a convention begin variable names with a capital letter. That is reserved for Class names, a concept that will arise once we reach the unit on Object Oriented Programming. For the sake of convention, always begin variable names with a lowercase letter.

camelCase is the typical form used in JS.

yearBorn
birthYear

Finally, we should avoid the use of reserved words in variable name assignment else we accidently overwrite an important component of the language base. while is a keyword used in while loops, and although While is not the same, it is close enough to similar that it should be on the unused words list.

Perhaps you were looking at ,future, past and present in your meaning. At any length, rewriting your code with common terms might make it easier to read and understand.

function howOld(year, age){
  const currentYear = 2019;
  const birthYear = currentYear - age;
  const future = year - birthYear;
  const past = birthYear - year;
  const present = year - birthYear;
  if (year > birthYear && year < currentYear) {
    return `You were ${present} in the year ${year}`;
  } else if (year > birthYear) {
    return `You will be ${future} in the year ${year}`;
  } else if (year < birthYear) {
    return `The year ${year} was ${past} years before you were born`;
  } else {
    return `error`;
  }
}
console.log(howOld(2018, 19))

First order of business will be to check the instructions for the exact text expected, including the grammar. None of the return strings contains a full-stop (period at end of sentence). Something that simple will trip up the lesson checker.


#17

Thanks for helping me!
I correct the code, but I still can’t pass the exercise:

function howOld(year, age){
const currentYear = 2019;
const born = currentYear - age;
const future = year - born;
const Past = born - year;
const present = year - born;
if (year > born && year <= currentYear) {
return You were ${present} in the year ${year}
} else if (year > born) {
return You will be ${future} in the year ${year}
} else if (year < born) {
return The year ${year} was ${past} years before you were born
} else {
return ‘error’
}
}

console.log(howOld(2060, 19))


#18

Try changing that to past so it matches the variable in your string literal.


#19

The code is working but still, I can’t pass the exercise.

const howOld = (year, age) => {
const currentYear = 2019;
const bornYear = currentYear - age;
let futureAge = year - bornYear;
let pastYear = bornYear - year;
let presentAge = year - bornYear;
if (year > bornYear && year < currentYear) {
return You were ${presentAge} in the year ${year}
} else if (year > currentYear) {
return You will be ${futureAge} in the year ${year}
} else if (year < bornYear) {
return The year ${year} was ${pastYear} years before you were born
} else {
return “error”
}
}

console.log(howOld(1990, 19)); // prints: The year 1990 was 10 years before you were born


#20

Are you getting an SCT error? How does it read?

The above is exclusive of both bornYear and currentYear. Should they be included in one or both comparisons?