Training Days Project spitting out different results


#1

I’m on the last couple steps of the Training Days project and I can’t figure out whats going wrong. It happens after I copy and paste Warren’s part. Running his parts through the function yields the SAME event as Nala, even after I do step 11 (moving the random into local scope). But also, the days for that event are wrong. Like Nala’s marathon will have 50 days, but Warren’s marathon will say 200. I don’t get why that is.

Also, as a side question, how do Warren’s parameters (with a “2” attached) get passed through when the parameters of the function itself not have the "2’?

Any help as appreciated, thanks!

// 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;
};

// The scope of `name` is too tight 
const logEvent = (name, days) => {
  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(name, event);
logTime(name, days);


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

logEvent(name2, event2);
logTime(name2, days2);

#2

In order to ensure we have the correct insight into this project, please post a link to the page. One wouldn’t want to suggest something that is not in the instructions.


#3

The value only of name2 is what get passed to the function, not the variable, itself.

When we consider it, random does not need to be a constant, and probably shouldn’t be. To scope it to the function, we use var or let, though the former is more fitting. let is for inside loops to give variables block scope and prevent them leaking into local scope. We achieve that in function scope with var.

var random = // ...;

Are the instructions explicit about using an if statement? Or are you free to choose? A convenient alternative to if..else if..else is switch..case..default which even looking at side-by-side here we can see the obvious similarity. Either, or, is fine. There is an even simpler way that needs no logic, but that will have to wait.

switch(random) {
  case 0: return "Marathon';
  case 1: return 'Triathlon';
  default: return 'Pentathlon';
}

edited case expressions. My bad.


#4

https://www.codecademy.com/courses/learn-javascript-scope/projects/training-days?action=resume_content_item&course_redirect=introduction-to-javascript

here is the link, thanks for your help


#5

I figured it out.

on the logEvent function, my parameters are wrong: (name, days), but the function block uses (name, event). Once I switched it to the correct parameters of (name, event) the function got the result I was looking for.

Thanks for the help!


#6

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