Here’s my code for the Training Days project:
(post deleted by author)
Hello Peter and fellow coders,
What I’m struggling to grasp with this project and other examples like it, is how does the programme know how to define the ‘event’ variable and therefore pass it on to other blocks?
In other words, the first block of code defines the function for ‘getRandEvent’, but at no point does it say that event = marathon (or pentathlon or triathlon).
So when it gets to the block of code for getTrainingDays and we are feeding/defining ‘event’ as a parameter, how does the code know that event has been assigned as marathon/triathlon/pentathlon from the previous block of code when ‘event’ hasn’t been explicitly named/defined in the first block of code?
I know that const event = getRandEvent(); is declared towards the end of the code along with const days = getTrainingDays(event);…but if the code is read from top to bottom then I’m struggling to understand why this isn’t done much earlier in the code
Thanks in advance for your time and consideration!
Hi, I wonder if anyone can help.
After watching the solution video, I still don’t understand the code. Why Event can be identified without defining it?
the function getRandEvent doesn’t define/name the variable Event?
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’;
}
};
Why would the following code understand the value of Event? Thanks!
Only thing we can think of would be in assignment of the return value:
let event = getRandEvent()
peterkibuchi - thanks so much for taking the time to write such a clear and easy-to-follow explanation.
The part which really helps it to make sense is to “think of a variable as a mere container for a value; and a function parameter as a mere placeholder for an argument that will be passed later when calling the function; and a function as a block of code that does something then returns a value”…as you can then start to understand why a function parameter can really take any name
I keep on getting an undefined value for the number days on step 8 even though I did what the guy did in the project walkthrough
Here is my code:
// The scope of random
is too loose
const random = Math.floor(Math.random() * 3);
const getRandEvent = () => {
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 = (name, 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);
In the code you have, the days
variable is declared inside the getTrainingDays
function.
So you can only use it inside that function. (days
has function scope.)
But the code uses days
outside the function, so that’s a problem when you do
logTime(name, days);
You can fix that by changing to scope of variable days
(to be global scope),
meaning
having let days;
be outside the getTrainingDays
function (before that function).
That way, days will work everywhere, not only inside the getTrainingDays
function.
now it says that days has already been declared after I did what u said
You can do a version where variable days
has global scope;
but you’d only declare it once so
you wouldn’t have the const days
in
const days = getTrainingDays(name, event);
code
const getRandEvent = () => {
// generate random number 0 to 2
const random = Math.floor(Math.random() * 3);
if (random === 0) {
return "Marathon";
} else if (random === 1) {
return "Triathlon";
} else if (random === 2) {
return "Pentathlon";
}
};
// days variable has global scope
let days;
const getTrainingDays = (name, event) => {
if (event === "Marathon") {
days = 50;
} else if (event === "Triathlon") {
days = 100;
} else if (event === "Pentathlon") {
days = 200;
}
return days;
};
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`);
};
// Define a name variable. Use it as an argument
const name = 'Nala';
const event = getRandEvent();
getTrainingDays(name, event);
logEvent(name, event);
logTime(name, days);
Or you could do a version where there is a days
that has function scope;
meaning declaring days
in the getTrainingDays
function.
code
const getRandEvent = () => {
// generate random number 0 to 2
const random = Math.floor(Math.random() * 3);
if (random === 0) {
return "Marathon";
} else if (random === 1) {
return "Triathlon";
} else if (random === 2) {
return "Pentathlon";
}
};
const getTrainingDays = (name, event) => {
let days;
if (event === "Marathon") {
days = 50;
} else if (event === "Triathlon") {
days = 100;
} else if (event === "Pentathlon") {
days = 200;
}
return days;
};
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`);
};
// Define a name variable. Use it as an argument
const name = 'Nala';
const event = getRandEvent();
const days = getTrainingDays(name, event);
logEvent(name, event);
logTime(name, days);
In either case, you need to declare the name
variable before you use it as an argument for a function.
And , in the getRandEvent
function you’ll need to generate the random number used (the variable random
).
// generate random number 0 to 2
const random = Math.floor(Math.random() * 3);
You can even go a step further, and have name
and days
be variables that only have global scope.
To do this, you’d declare days
before any function uses days
, and remove days
as a parameter for any functions and as an argument anywhere a function is called.
code
const getRandEvent = () => {
// generate random number 0 to 2
const random = Math.floor(Math.random() * 3);
if (random === 0) {
return "Marathon";
} else if (random === 1) {
return "Triathlon";
} else if (random === 2) {
return "Pentathlon";
}
};
// days variable has global scope
let days;
const getTrainingDays = (event) => {
if (event === "Marathon") {
days = 50;
} else if (event === "Triathlon") {
days = 100;
} else if (event === "Pentathlon") {
days = 200;
}
return days;
};
// Define a name variable before it is used.
const name = 'Nala';
const logEvent = (event) => {
console.log(`${name}'s event is: ${event}`);
};
const logTime = () => {
console.log(`${name}'s time to train is: ${days} days`);
};
const event = getRandEvent();
getTrainingDays(event);
logEvent(event);
logTime();
You could even do the same with the event
variable, if you want.
code
// event variable has global scope
let event;
const getRandEvent = () => {
// generate random number 0 to 2
const random = Math.floor(Math.random() * 3);
if (random === 0) {
event = "Marathon";
} else if (random === 1) {
event = "Triathlon";
} else if (random === 2) {
event = "Pentathlon";
}
return event;
};
// days variable has global scope
let days;
const getTrainingDays = () => {
if (event === "Marathon") {
days = 50;
} else if (event === "Triathlon") {
days = 100;
} else if (event === "Pentathlon") {
days = 200;
}
return days;
};
// Define a name variable before it is used.
const name = 'Nala';
const logEvent = () => {
console.log(`${name}'s event is: ${event}`);
};
const logTime = () => {
console.log(`${name}'s time to train is: ${days} days`);
};
getRandEvent();
getTrainingDays();
logEvent();
logTime();
You can mix different approaches too …
having a version of the variable that has global scope, and having another version of the variable that has function scope (which would mean it would actually be a separate variable) in situations where that is convenient.
I don’t understand why moving const random = Math.floor(Math.random() * 3); from global to inside the logTime function makes it so that the number is different for Nala and Warren. Can anyone explain in a non-jargony way? Like does it make it so the const random = Math.floor(Math.random() * 3) doesn’t run until it is called with a value as part of a function? so it is called twice rather than just once?
Wen the random function is run it creates a random number variable. This is used throughout the program. If we want a different random number we have to run it again. To do this we have placed the random function inside of another function to have it called every time we call that function thereby assigning a new random number.