FAQ: Code Challenges: JavaScript Fundamentals - howOld()

I need some help, my code is working perfectly but I still get this message.
thanks in advance.

Hello, @gaby_bendfinger.

Welcome to the forums. I can’t copy, and run your code since it’s in a screenshot, but running through it in my head, the logic seems correct. The problem is likely that you have single quotes ' ' inside your back ticks, so your output is:

‘bla bla bla’

instead of

bla bla bla

The SCT for the lesson probably isn’t expecting the quotation marks.

Thanks midlindner!

I appreciate the help, it works!

1 Like

I’m getting an error and can’t figure out why. I’d love help if possible.

const howOld = (age,year) => {
  const currentYear = 2020;
  const bornYear = currentYear - age;
  const newAge = Math.abs(year-bornYear);
  if (newAge > age) {return `You will be ${newAge} in the year ${year}`;} 
  else if (year < bornYear) {return `The year ${year} was ${bornYear - year} years before you were born`;} 
  else {return `You were ${newAge} in the year ${year}`;
     };
}


console.log(howOld(33,1980));

error = If the year passed in was before the person was born, the function should return: ‘The year [year passed in] was [calculated number of years prior to their birth] years before you were born’

Hello, @net6465339463, and welcome to the forums.
In the string you are returning below, you have already calculated the correct value for the number of years before the user was born, and assigned it to the variable newAge. Not sure if using ${newAge} in place of ${bornYear - year} will clear the error. It appears you are producing the correct output.

Edit: I just looked at the exercise, and the SCT for the lesson checks your code using howOld(3, 1998). Try it, and you’ll see the expected output is not returned.

I’m getting this error message when I run my code:

If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’

My code looks like this:

// Write your function here:
const howOld = (age, year) => {
  const thisYear = 2019;
  const birthYear = thisYear - age;
  
  if (year > thisYear) {
    return `You will be ${year - birthYear} in the year ${year}`;
  } else if (year < birthYear) {
    return `The year ${year} was ${birthYear - year} years before you were born`;
  } else {
    return `You were ${year - birthYear} in the year ${year}`;
  }
}

// Once your function is written, write function calls to test your code!
console.log(howOld(40,2029))
console.log(howOld(40,1969))
console.log(howOld(40,1989))

Running my three test function calls seems to suggest I have the correct solutions:

You will be 50 in the year 2029
The year 1969 was 10 years before you were born
You were 10 in the year 1989

I seem to have a habit of missing the obvious stuff…anyone give me a hint?

EDIT original had wrong code block

1 Like

Hello, @laconicsky.

Your code does product correct results. The issue is that the SCT for the lesson is written to get the current year, and assign it to the value of thisYear. You are using 2019, but the SCT is using 2020. If you change your code to assign 2020 to thisYear, it should pass the SCT. Obviously this exercise doesn’t account for whether or not the person specified has already had their birthday or not in the current year. :wink:

1 Like

What does SCT stand for?
And yeah I think I just forgot it was 2020…

SCT is Submission Correctness Test. You can actually view the test.js file for this particular exercise by clicking on the folder icon in the top left corner of the editor, and clicking on it.

1 Like

Thanks that’s really useful to know.

Because the Date object has probably not been introduced yet, we won’t think to use it to generate the current year. It helps to make our program more dynamic, though.

let currentYear = new Date.getFullYear()
2 Likes

That’s correct - on the web dev path we haven’t come across Date yet. Useful to know though thanks.

1 Like

// Write your function here:

const howOld = (age, year) => {
let currentYear = 2020;
if (year >= currentYear) {
return You will be ${(year - currentYear) + age + 1} in the year ${year};
} else if ((currentYear - year) > age) {
return The year ${year} was ${currentYear - age - year - 1} years before you were born;
} else {
return You were ${-1*(currentYear - year - age)+1} in the year ${year}
}
}

// Once your function is written, write function calls to test your code!
console.log(howOld(36, 2023));
console.log(howOld(36, 1983));
console.log(howOld(36, 1985));
console.log(howOld(36, 1966));

You will be 40 in the year 2023
The year 1983 was 0 years before you were born
You were 2 in the year 1985
The year 1966 was 17 years before you were born

this is my code, without any additional variables, it’s working fine, why i can’t go to next exersise?

Thank you :(((((((((

const howOld = (age, year) => {
let currentDate = new Date();
let currentYear = currentDate.getFullYear()
let yearOfBirth = currentYear - age;
if (year > currentYear){
return 'You will be ’ (year - yearOfBirth) + ’ in the year ’ + year;
} else if (year < yearOfBirth){
return 'The year ’ + year + 'was ’ + Math.abs(year - yearOfBirth) + " years before you were born";
} else {
return 'You were ’ + (year - yearOfBirth) + ’ in the year ’ + year;
}
}

That’s my code and it seems to give me the right answers. However, it still doesn’t pass the browser tests. Does anyone see something that I don’t?

Thanks a lot!

Troubleshooting my code

This appears to work, yet I cannot move on in the lesson.

const howOld = (age, year) => {
  const theCurrentYear = 2020;
  const yearDifference = year - theCurrentYear;
  const newAge = age + yearDifference;
  if (yearDifference > 0) {
    return `You will be ${newAge} in the year ${year}.`;
  } else if (yearDifference< 0) {
    return `The year ${year} was ${Math.abs(yearDifference)} years before you were born.`;
  } else if (yearDifference === 0) {
    return `You were ${age} in the year ${year}.`;
  }
};

i get this code as output, which is correct.

You will be 26 in the year 2022.
You were 24 in the year 2020.
The year 2018 was 2 years before you were born.

Oh, but are we to be sure of that? If the information is correct, you “are” 24 in 2020, not “were” and 2018 would be 22 years after you were born, not 2 years before.

There appear to be some discrepancies that need addressing, here.