Training Days: Incorrect days value in output for competitor

When I add more competitors to test the output shows the same value for ‘days’ even though events are different. It looks like the variable ‘days’ is set one time in the first run only and I don’t know why.

Here is the code I have by the end of this project:

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

const getTrainingDays = event => {
  let days;
  if (event === 'Marathon') {
    days = 50;
  } else if (event === 'Triathlon') {
    days = 100;
  } else if (event === 'Pentathlon') {
    days = 200;
  } else {
    return 'Not sure how many '
  }

  return days;
};

const logEvent = (name, event) => {
  console.log(`${name}'s event is: ${event}`);
};

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


const event = getRandEvent();
const days = getTrainingDays(event);
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);

const event3 = getRandEvent();
const days3 = getTrainingDays(event3);
const name3 = 'John';

logEvent(name3, event3);
logTime(name3, days3);

Here are the outputs I see when I test by adding more competitors:

Output 1:

Nala's event is: Pentathlon
Nala's time to train is: 200 days
Warren's event is: Triathlon
Warren's time to train is: 200 days
John's event is: Marathon
John's time to train is: 200 days

Output 2:

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

Output 3:

Nala's event is: Pentathlon
Nala's time to train is: 200 days
Warren's event is: Pentathlon
Warren's time to train is: 200 days
John's event is: Triathlon
John's time to train is: 200 days

Hey there, welcome to the forums!

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

${name} you have it, but where would ${days} come from?

1 Like

Ah I see :man_facepalming: This is how it should’ve been:

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

Interesting that it still adds days even though there was no parameter pass into the function.