Troubleshooting my code?

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))

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.

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))

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

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

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?

This is the code I wrote, I don’t receive an sSCT error. Yes both bornYear and currentYear need to be true, otherwise it wil output the futureAge. But since we need an outpout with the age from the past when we were younger, I wrote it this way. Sorry for my bad english.