Training Days


#1

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

Final Step: 12

How does code act?

This is the second time I’ve gone through this lesson from step one, and both times I’ve ended up with the exact same error. If it randomly chooses Marathon, it works, randomly landing on Triathalon or Decathalon produces the following error.

/home/ccuser/workspace/learn-javascript_scope-training-days/trainingDays.js:34
return activities.join(’, ');
^

TypeError: Cannot read property ‘join’ of undefined
at getEventActivities (/home/ccuser/workspace/learn-javascript_scope-training-days/trainingDays.js:34:20)
at getEventMessage (/home/ccuser/workspace/learn-javascript_scope-training-days/trainingDays.js:50:86)
at Object. (/home/ccuser/workspace/learn-javascript_scope-training-days/trainingDays.js:53:1)
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)



//all events array
const getAllEvents = () => {
  return ['Marathon', 'Triathalon', 'Decathalon'];
};

//selects EVENT at random from the array
const getRandomEvent = () => {
  const allEvents = getAllEvents();
  const event = allEvents[Math.floor(Math.random() * allEvents.length)];
  return event;
};

//returns a LIST of ACTIVITIES based on the event selected
const getEventActivities = event => {
  const allEvents = getAllEvents();
  if (!allEvents.includes(event)) {
    return null; 
  }
  
  let activities;
  if (event === 'Marathon') {
    activities = ['Running'];
  } 
  else if (event === 'Triathlon') {
    activities = ['Running', 'Cycling', 'Swimming'];
  } 
  else if (event === 'Decathlon') {
    activities = ['Running', 'Hurdles', 'Javelin throw', 'Discus Throw', 'Shot put', 'High Jump'];
  }
  return activities.join(', ');
};

//returns the NUMBER OF DAYS to train based on the event selected
const getDaysToTrain = event => {
  const allEvents = getAllEvents();
  allEvents ['Marathon', 'Triathlon', 'Decathlon'];
  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(myEvent) + '. You have ' + getDaysToTrain(myEvent) +  ' days to train.');
}

getEventMessage();





#2

You misspelled Triathalon & Decathalon in your if statements from when you declared the array up top.

You are missing the ‘a’ before ‘lon’;


#3

Always going to be something easy, isn’t it. Thank you for the help!


#4

It’s always the first thing I check for on this forum.

I’m sure it was the lesson that instructed you to write this code, but there would be a much more efficient way that would’ve done more to avoid the spelling mistake.

Taking two arrays and comparing arrays and comparing array B for a match in array A.

// Written the same as originally
const events = ["Marathon", "Triathalon", "Decathalon"]
// Group activities in array and use their index as an ID for matching with the index of events.
const activities = [
 ["Running"],
 ["Running", "Cycling", "Swimming"],
 ["Running", "Hurdles", "Javalin Throw", "Discus Throw", "Shot Put", "High Jump"]
]
const days = [50, 100, 200]
let ranKey = Math.floor( Math.random() * events.length )
console.log( "You are registered for the " + events[ranKey] + ". The activities you will be participating in for this event are: " + activities[ranKey].join(", ") + ".")

Glad to be of help!


#5

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