PROJECT: Training Days


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

I’m trying to complete this project:

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

I’m receiving these error messages:

/home/ccuser/workspace/learn-javascript_scope-training-days/trainingDays.js:59
console.log('Your event is a ’ + myEvent + '. Your event activities consist of ’ + getEventActivities() + ‘. You have ’ + getDaysToTrain() + ’ days to train.’);
^

ReferenceError: myEvent is not defined
at Object. (/home/ccuser/workspace/learn-javascript_scope-training-days/trainingDays.js:59:34)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

let getAllEvents = () => {
  return ['Maraton', 'Triathlon', 'Decathlon'];
};


/* this is used a few places and it is vulnerable */ 
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'];
     
  } 
  if (event === 'Triathlon') {
    activities = ['Running', 'Cycling', 'Swimming'];
     
  } 
  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();
}

getEventMessage();

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

What do I need to do?

Thanks!


#2

Look at the error, as it’s pointing out the problem:

It’s telling you that myEvent is not defined where you used it on line 59. Look at your code.

Where did you declare and define that variable? I see it within the function getEventMessage(). But your message being sent to the console is in global scope.


#3

The function name assumes that it will output the message, so you need to move the entire log into the function, then you will have access to the local variable.

So it will be like this:

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();

getRandomEvent();


#4

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