Here’s my code for the Dog Years project:
// my current age
const myAge = 20
// first2 years
let earlyYears = 2
earlyYears* = 10,5
let laterYears = myAge -2
//Dog years older than 2
laterYears* = 4
console.log(earlyYears)
console.log(laterYears)
//tranlate age
let myAgeInDogYears = “earlyYrs”+“laterYears”
const myName = “Luna”
console.log(“my name is ${myName}. Iam ${myAge} which is ${myAgeInDogYears} in dog years”)
But mine does not work! If I run it, I get a syntax error .
Several issues found:
earlyYears* = 10,5
should be
earlyYears *= 10.5
Notice that there’s no space between *
and =
Also, here:
let myAgeInDogYears = "earlyYrs"+"laterYears"
those shouldn’t be strings (they should be the earlyYears
and laterYears
variables from earlier in the code) so the quotation marks don’t belong there.
And,
console.log("my name is ${myName}. Iam ${myAge} which is ${myAgeInDogYears} in dog years")
Those are the wrong kind of quotation marks for ${stuff}
to work. (You want ` instead of "
.)
Thank you so much, it was getting late and I didn’t see any differences anymore.
The instructed code doesn’t account for ages below 3. Here’s the code I made with conditionals that allow for any age below 3 to produce the proper output:
// My current age.
const myAge = 1;
// First 2 years of a dog’s life.
let earlyYears = 2;
// Accounts for very young ages.
if (myAge === 1) {
earlyYears = 1;
}
else if (myAge < 1) {
earlyYears = 0;
}
earlyYears = earlyYears * 10.5;
// Every year after the first 2 years of a dog’s life.
let laterYears = myAge - 2;
if (laterYears < 1){
laterYears = 0;
}
// Translates human years into dog years.
laterYears *= 4;
/* Checking script so far.
console.log(earlyYears)
console.log(laterYears)
*/
// Adds together early years and later years.
let myAgeInDogYears = earlyYears + laterYears;
// My current Name, lowercased.
let myName = ‘Doug’.toLowerCase();
// Prints all variables into string.
console.log(My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} years old in dog years.
);