Help with trainingDays project

I’m working through the trainingDays project here: https://www.codecademy.com/courses/introduction-to-javascript/projects/training-days

and here’s the code for it:

// 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;
};
const name = 'Nala';
// The scope of `name` is too tight 

const logEvent = event => {
  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);

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

and I get this in return:

Nala's event is: Triathlon
Nala's time to train is: 100 days
Nala's event is: Warren
Nala's time to train is: Warren days

the calls for logEvent and logTime for the first person, Nala looks right. The calls for event2 and name2 for Warren are somehow mixing up Nala and Warren. Is scope getting messed up?

here:

const logTime = days => {
  console.log(`${name}'s time to train is: ${days} days`);
};

using a global variable (name) in the function is a bad idea.

name should be a parameter. You seem to intent this, because here:

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

you call the function with two arguments.

In part I blame JavaScript here, all other languages I worked in would have thrown an error for the fact that the number of (positional) arguments does not match the number of parameters.

1 Like

yea, that’s what it seems like to me. I ran my code for the exercise in a terminal Node window and it gives me the same issue. Time for a bug report.

You are going to report a bug in your own code? Bug reports are for platform/codecademy related issues, that is not the case here. We also can’t do anything about shortcomings of the JavaScript language

1 Like