Question about this project

Thisis the course project I am referring to. I am not struggling to complete the project, as a matter of fact I fixed the whole thing in a couple of minutes. I did it partially by accident by misreading the first step, and doing something different then I guess I was supposed to. My question is what is the point of this lesson, and all of the seemingly unnecessary steps? Maybe I’m missing some big piece of information regarding js, and even though what I did works great on codeacademy but in practice not so much. I know scope is an integral part of a good working program and that it can get really hairy at times if you’re not careful and don’t understand it, so I would like to avoid bad habits and learning things incorrectly. So you can see what I did I will past my code below:

// The scope of random is too loose

name = ‘Nala’

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 = () => {
let days = event;
if (event === ‘Marathon’) {
return days = 50;
} else if (event === ‘Triathlon’) {
return days = 100;
} else if (event === ‘Pentathlon’) {
return days = 200;
}
};

// The scope of name is too tight
const logEvent = () => {

console.log(${name}'s event is: ${event});
};

const logTime = 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(event);
logTime(days);

Are you sure you pasted in the right thing? Because that code is not going to function how one may hope…

If you’re certain it’s all groovy, some things for you to ponder:

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

Regarding that first line - how is this function receiving its parameters?
Regarding that second line, what values are these variables expected to be used for? Should ‘days’ ever have the same value as ‘event’?

1 Like

Yes, and that was kind of what I was getting at with this post. If you’re not familiar with this particular lesson it starts with prewritten code that when you run it comes back with an error regarding scope of ‘days’ in the getTrainingDays function. I made one or possibly two changes to it to get it to “work” and it seems to function as it should. This all seemed far too simple in my opinion for it to be the point of the lesson.

Its tough because I probably wouldn’t have written the code in this way if it were one of the typical projects you do one here, not that my way is superior or even right for that matter. I could tell something was off but the code, but it functions the way it should when using the site.