Problem with trainindays.js output


#1

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

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
After Step 7 in the “Training Days” project, my output contains two null values and I can’t figure out why. I’ve tried resetting the page, but I achieved the same results. This output occurs when I pass event as a parameter to getEventActivities() and getDaysToTrain() functions. The first time I tried the exercise, I thought the following steps would eventually lead to an expected output. However, this was not the case. Following is my entire code for the project. I don’t understand! I’m also a newbie, so if there is an obvious mistake, please point it out :slight_smile:

Thanks!
Ashley

const getAllEvents = () => {
  return ['Marathon', 'Triathlon', 'Decathlon'];
};

const getRandomEvent = () => {
  const allEvents = getAllEvents();
  const event = allEvents[Math.floor(Math.random() * allEvents.length)];
  return event;
};

const getEventActivities = event => {
  const allEvents = getAllEvents();

  if (!allEvents.includes(event)) {
    return null;
  }
    
  let activities;
  if (event === 'Marathon') {
    activities = ['Running'];
    return activities.join(', ');
  } 
  if (event === 'Triathlon') {
    activities = ['Running', 'Cycling', 'Swimming'];
    return activities.join(', ');
  } 
  if (event === 'Decathlon') {
    activities = ['Running', 'Hurdles', 'Javelin throw', 'Discus Throw', 'Shot put', 'High Jump'];
    return activities.join(', '); 
  }

};

const getDaysToTrain = event => {
  const allEvents = getAllEvents();
  if (!allEvents.includes(event)) {
    return null; 
  }
  const eventTrainingTimes = {'Marathon': 50, 'Triathlon': 100, 'Decathlon': 200 };
  return eventTrainingTimes[event];
};

const getEventMessage = () => {
  const myEvent = getRandomEvent();
	console.log('Your event is a ' + myEvent + '. Your event activities consist of ' + getEventActivities() + '. You have ' + getDaysToTrain() +  ' days to train.');
};

getEventMessage();



#2

Remember to pass myEvent to both of those functions.


#3

I’m sorry if I seem to be asking a question which should have an obvious answer, but exactly where/how do I pass myEvent to both functions? I’ve tried everything I can imagine (remember I’m a newbie!!! :slight_smile: ) and it either returns the same null values or an error.


#5

In the above code you have ‘event’ set as a parameter for getEventActivities and getDaysToTrain. Which is nicely done, but you must remember that parameters need to receive their value from an argument and you give an argument when you call the function like so:
functionNameHere (argumentGoesHere)
So you need to use that myEvent variable as an argument in both your functions when you call them at the end so that the event parameters will get values and not run
if (!allEvents.includes(event)) {
return null;
}


#6

Thank you so much! That worked perfectly (as I’m sure you knew it would :blush: ).


#7

Sorry I didn’t understand this the first time! Now that I’m reading it (after reading the other explanation provided by aucrown), your solution is blatantly obvious… :confused:


#8

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


#9