Training Days function parameter

Training Days practice project

Here’s the link to the project https://www.codecademy.com/projects/practice/training-days

Overall I’m quite ok with the project, I had to go back a few times but it’s getting clearer, the only thing that I cannot understand it’s the parameter event in the function:

const getTrainingDays = event => {
let days;
if (event === ‘Marathon’) {
days = 50;
} else if (event === ‘Triathlon’) {
days = 100;
} else if (event === ‘Pentathlon’) {
days = 200;
}
return days;
};

Why do we need to pass the parameter event in the first line? I tried removing it and still works fine, I’m new and I’m guessing I’m probably missing something, but I can’t get around. Any input?

Kind regards.

Hi,
if you say that your program still works even after removing the parameter ‘event’ you probably declared ‘event’ globally (somewhere above the function call).
Then the function has access to the variable ‘event’ and your code works. If you remove the global declaration of ‘event’ your program will throw a reference error.
Defining the variable globally is not best practice as your program would be more generic if you pass event as an argument.

These are the 2 possible ways:

const getTrainingDays = () => {
let days;
if (event === 'Marathon') {
days = 50;
} else if (event === 'Triathlon') {
days = 100;
} else if (event === 'Pentathlon') {
days = 200;
}
return days;
};
const event = 'Triathlon'
console.log(getTrainingDays())

This would be better:

const getTrainingDays = (event) => {
let days;
if (event === 'Marathon') {
days = 50;
} else if (event === 'Triathlon') {
days = 100;
} else if (event === 'Pentathlon') {
days = 200;
}
return days;
};
console.log(getTrainingDays('Triathlon'))

Hi, thanks for the reply, I’m not sure if you have checked the project, but yes, the variable event it’s global, but because another function generates it.

Here is the complete code:

// The scope of `random` is too loose 


const getRandEvent = () => {
  const random = Math.floor(Math.random() * 3);
  if (random === 0) {
    return 'Marathon';
  } else if (random === 1) {
    return 'Triathlon';
  } else if (random === 2) {
    return 'Pentathlon';
  }
};

// The scope of `days` is too tight 
const getTrainingDays = (event) => {
  let days;
  if (event === 'Marathon') {
    days = 50;
  } else if (event === 'Triathlon') {
    days = 100;
  } else if (event === 'Pentathlon') {
    days = 200;
  }

  return days;
};
const name = 'Nala';
// The scope of `name` is too tight 
const logEvent = (name, event) => {
  console.log(`${name}'s event is: ${event}`);
};

const logTime = (name, days) => {
  console.log(`${name}'s time to train is: ${days} days`);
};

const event = getRandEvent();
const days = getTrainingDays(event);
// Define a `name` variable. Use it as an argument after updating logEvent and logTime 


logEvent(name, event);
logTime(name, days);

const event2 = getRandEvent();
const days2 = getTrainingDays(event2);
const name2 = 'Warren';
 
logEvent(name2, event2);
logTime(name2, days2);

And as always, after searching for help, I found more clarity, I was expecting to get an error when removing the parameter event, but I didn’t, but looking more carefully, I noticed that by removing the event, the code gives the same days for both names and they are not following the if statement.

You call getTrainingDays with the argument event2. Then the value of the parameter event equals event2. If you remove the parameter event from the function definition, the function will be executed with the value of event.
If you hardcode the variables like this:

const event = 'Marathon';
const event2 = 'Triathlon';

you’ll see that the result is not the same if you remove the parameter.

1 Like