JavaScript Syntax Introduction - Dog Years

Hi everyone :world_map:! I’m new to programming and would like to find people to discuss and learn together. I currently live in Spain and speak English and Spanish!

So now… some coding exercises from the Full Stack Engineer path. These are two approaches to the dog years project.

This is the default solution:

//Enter the human's age
const myAge = 10;
//The first 2 years count differently for the dog's age calculation
let earlyYears = 2;
earlyYears = earlyYears * 10.5;
//Substract already accounted years in earlyYears var
let laterYears = myAge - 2;
//laterYears count as 4 years in dog years
laterYears *= 4;
//Let's add up both calculations
let myAgeInDogYears = earlyYears + laterYears;
//Make all letter lowercase
let myName = 'Gustavo'.toLowerCase();
//Using string interpolation to embed var into a string and print
console.log(`My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} years old in dog years.`)

And here is another approach I found easier and more straight forward:

//Human's age to calculate, human's name and a var myAgeInDogYears to store the result
const myAge = 33;
const myName = 'Gustavo'.toLowerCase();
let myAgeInDogYears;

//Simple if...else statement to separate between people age 2 and under and people age 3 and over
if (myAge < 2) {
myAgeInDogYears = myAge * 10.5;
} else {
myAgeInDogYears = (myAge - 2) * 4 + 21;
 }

//String interpolation to embed var and const
console.log(`My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} old in dog years.`);

Appreciate any feedback! :studio_microphone:

Hey there, nice to meet you!
I think your solution makes an awful lot of sense to be honest. I assume they use that method for teaching reasons, but I find yours much more readable!

Only other thing I would consider would be wrapping it all in a function so that you don’t pollute the global scope, but that’s outside the scope (no pun intended) of this exercise.

:joy: :joy: :joy:

Thanks for the review @data2704285749!

Just read about scopes and sure makes a lot of sense to avoid scope pollution! I will keep it in mind!

the problem with this approach:

myAgeInDogYears = (myAge - 2) * 4 + 21;

is that you have a lot of “magic” numbers. What is 2? what is 4? what is 21? You know now, but do you still remember in 3 months time? half a year? a year?

And what of another developer taking over your project, they have no clue what those “magic” numbers are. the variables and comment help to remind us

Shorter code does not always mean code that is easier to read and maintain. Finding a balance is important, you can do your approach, but then you need to put a comment what the numbers are

putting the variable at the top of the program also enable us to quickly tweak anything about the calculation. If numbers are repeated without variable, this process becomes a lot more difficult and might introduce bugs.

I definitely see what you’re saying! It makes so much sense to keep the code easy to understand, for myself and others! And while I understand the concept I find it a little abstract.

@stetim94 do you have a rule of thumb for balancing readability and scope pollution?
Do you read over your code after a few days (or hours) to check if you can easily understand it?

I’ll keep working on finding that balance!
Thanks for taking the time!!! :+1:t4: :clap:t4:

It is abstract, but something which is worth learning in the long run. You first learn how to program, then you learn how to write clean, readable and maintainable code.

Try avoiding that lines are more then 80 characters. Look how code is written in documentation for the language.

sometimes, yes. But more importantly, I try separation of concerns. When I write a function/method, the function should have a single responsibility and should be easy test. The function should also have a logic/descriptive name, so you can look at the function name, and understand what the function does without having to study underlying implementation in detail. Speak of abstract haha

1 Like