My age in dog age calculator

Hey there,

Just finished the project ‘Dog Years’.

My question is what happens if my age is less than 2? When I set my age as ‘1’ the calculator show my dog age is 17 instead of 10.5, because we define the later years as ‘myage - 2’ so basically in this case it will be -1, and then -1*4 = -4. Is there any formula which ignores the negative value? like “IF” or something like that.


Could you post the link/url of the exercise/project and your code please? Thank you :slight_smile:

1 Like

Is there something wrong with if? If not, use it, right? Or if there is, then, what? That would say something about what you’re after.
Or are you asking what should be done for lower amounts? You could google “dog years”

Dude, I have just started the course introduction to java script and this was in the very beginning, a project called Dog years.

I don’t know if ‘IF’ even exits in Java, if so, how to use it.


Here it is. With ‘33’ it works perfectly. If you use ‘1’ as myAge, the calculation is wrong:

const myAge = 33; // This is my age
let earlyYears = 2; // this is the early years
earlyYears *= 10.5;
let laterYears = myAge-2;
laterYears *= 4;
let myAgeInDogYears = earlyYears+laterYears;
let myName = ‘John Dog’.toLowerCase();
console.log(My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} years old in dog years.)

java and javascript are different languages

google: “javascript if”

Yeah, sorry, my bad, it is javascript.

I will check it, thanks. I thought this forum exactly exists for this reason (to get help) but then I will use google.

If there’s an immediate automated way to find something out, surely that is something to try before looking for people

people are good for when you don’t know how to continue from where you are

and, if someone asks something that can be googled I get very confused about what they’re looking for, surely it’s not the thing they can google, must be something else, but what?

and arguably almost any question around exercises should probably be in terms of “how do I find this out/how do I approach this problem” because the real problem is the method for getting to the result, the result itself is something trivial and worthless

You are right. I wrote to the forum because I was looking for some human interaction, especially because I thought this information could be useful for others as well. I am sure I am not the only one who faced this ‘issue’ or had this question in their mind.

no need to explain, I’m just looking to alter your perspective a bit, the way you view things is super important for problem solving

1 Like

While on the topic of perspective, your human perspective on a problem is often very useful as humans tend to solve a lot of problems unconsciously. If you figure out/describe how YOU would do something, then you’ve probably laid out an algorithm right there, and then you can write that down.

One approach to this dogyears problem is to have two expressions, one for young dogs and one for old ones, and you’d use an if-statement to decide which to use.

Another approach is to see the age as two parts, the first two years and the rest. They have different multipliers to them and are then added:

dogYears = earlyYears * 10.5 + lateYears * 4

earlyYears would be the full age but at most 2, so one would pick the smallest of these two values
lateYears would be the full age minus 2 but at least 0, so one would pick the largest of these two values

The functions Math.max and Math.min could be used to figure those out, and then a single expression that covers the full possible range has been obtained.

But I didn’t start out by thinking that I want to solve it with min/max functions, I decided what I wanted to do first, and then started looking for matching parts in the language.

You can use if…else statements in JS. They work like this:

if (var1 === condition) {
}else/*the else is optional*/{

In your scenario, you could use an if…else statement with the const myAge. For example you could code:

if (myAge <= /*less than/equal to*/ 2){