Training Days logic

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-scope/projects/training-days

Step 11 says that each time the random event variable is called it will generate the same number. Even when I used let instead of const Nala and Warren still had the same values. Those values would change but the would change together?

I’m trying to figure out why this is? I don’t understand the logic. This was never an issue in any of the previous exercised where math.random and math.floor are used frequently.

Thanks!!

We see that the random variable is defined in the global scope. Each time getRandEvent() is called, it uses the same value.

Nothing is generated except the random number at the start of the program. That value never changes. In order to have a new number each time we run the function we need to generate it within the context of that run time instance. A further benefit is that we remove one unnecessary global variable.

2 Likes

Consider,

const getRandEvent = () => ['Marathon', 'Triathlon', 'Pentathlon'][Math.floor(Math.random() * 3)];
const getTrainingDays = event => {
  return {'Marathon': 50, 'Triathlon': 100, 'Pentathlon': 200}[event];
}
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);
const name = 'Nala';
logEvent(name, event);
logTime(name, days);

This is code you will write with ease in due course. Learn how to reason and plan, and keep a pencil at hand. Learn how to express.

1 Like

After picking a random number you’ve got a number.
Looking at that number repeatedly isn’t going to change it, you’d need to pick a new number for that to happen.

Thank you! It makes perfect sense now.

1 Like

Hello mtf:

I am having some what similar problem with ‘Training Days’. By following through the steps 6 and 7 of the instructions my code produces incorrect results. This’s my code:

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;

};

// The scope of name is too tight

const name = ‘Nala’;

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

//const name = ‘Nala’;

logEvent(event);

logTime(days);

const event2 = getRandEvent();

const days2 = getTrainingDays(event2);

const name2 = ‘Warren’;

logEvent(name2, event2);

logTime(name2, days2);

And this’s the output when I run:

Marathon’s event is: undefined
50’s time to train is: undefined days
Warren’s event is: Triathlon
Warren’s time to train is: 100 day

As you see it renders the name “Nala” undefined in a wrong place. it also places ‘event’ and ‘day’ wrong places as well. Any hint regarding what I’m doing wrong here, please?

Please make an attempt at rendering your code sample readable. The new user section has advice in this regard. “Formatting Code Samples”, or some such.

Where is it? I see that it looks ugly as I directly copy and paste :grinning:

Did you find the post on how to format your code?

No sir. Is it the ‘Preformatted text’ - the button with the sign of ‘</>’?

Now you’re getting the idea. Did you read the new user topic?

No I didn’t read it.

Then please do. It is intended to guide you through the newbie phase of being a forum member, here.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.