Name2 appears as name?

const getRandEvent = () => {
let 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 = 0;
if (event === ‘Marathon’) {
days = 50;
} else if (event === ‘Triathlon’) {
days = 100;
} else if (event === ‘Pentathlon’) {
days = 200;
}
return days;
};

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
const name = ‘Nala’;

logEvent(event, name);
logTime(days, name);

const getRandEvent = () => {
let 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 = 0;
if (event === ‘Marathon’) {
days = 50;
} else if (event === ‘Triathlon’) {
days = 100;
} else if (event === ‘Pentathlon’) {
days = 200;
}
return days;
};

const logEvent = event2 => {
console.log(${name2}'s event is: ${event2});
};

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

const event2 = getRandEvent();
const days2 = getTrainingDays(event2);
const name2 = ‘Warren’;

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

logs as…

Nala’s event is: Nala
Nala’s time to train is: Nala days
Nala’s event is: Pentathlon
Nala’s time to train is: 200 days

What behavior are you expecting?

Also can you format your code?

1 Like

i know its something with logEvent and logTime but when i tried to duplicate the code with name2 and event2 it didnt seem to work

Make sure to wrap around everything in console log with backticks and here.

Then when the random is 0 it returns,

Nala's event is: Marathon
Nala's time to train is: 50 days
Nala's event is: Marathon
Nala's time to train is: 50 days

Is this what you are expecting?

// 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}); // `` doesnt appear
}

i need to adjust this to take in name and name2 as well as event and event2 seperate. when i tried to duplicate the code and edit the copy to name2 and event2 i pulled up an error.

i need to have logged:

Nala's event is: Pentathlon //these need to differ
Nala's time to train is: 200 days // from
Nala's event is: Pentathlon // these
Nala's time to train is: 200 days // .

You’re generating random only once and then reusing it in all calls, that’s why you get the same result.

when i separate them they work individually. how should i go about combining these?

I was wrong about the random function.
You didn’t pass name as parameter.

Possible solution: https://jsfiddle.net/um80z2na/ (I replaced if...else with object lookup, but it’s just a personal preference).

1 Like
const getRandEvent = () => { let 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 = 0;
if (event === ‘Marathon’) {
days = 50;
} else if (event === ‘Triathlon’) {
days = 100;
} else if (event === ‘Pentathlon’) {
days = 200;
}
return days;
};

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

const logTime = (name, 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
const name = ‘Nala’;

logEvent(event, name);
logTime(days, name);

const getRandEvent2 = () => {
let 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 = 0;
if (event === ‘Marathon’) {
days = 50;
} else if (event === ‘Triathlon’) {
days = 100;
} else if (event === ‘Pentathlon’) {
days = 200;
}
return days;
};

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

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

const event2 = getRandEvent2();
const days2 = getTrainingDays(event2);
const name2 = ‘Warren’;

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


this is what i did, still doesnt work. im gonna check that solution out

wow im glad i saw that, it is a much clearer code. thanks. best part is understand what you did lol