[JavaScript] Scope Project: Training Days - Resulting Nala Days

Hi everyone,

I’m at step#7, how come the result can be:

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

How come ‘Nala’ value can override ‘event’ and ‘days’ variables value?

Appreciate for the help. Thank you

const name = 'Nala'
// 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;

// 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(name, event);
logTime(name, days);

Hi mathsap,

if you pass botn name and days to the fuction logTime it works just fine:

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

Hello there,

Thank you for replying my question.
Yes, I’ve tried that by following the walkthrough video.
However, I just curious, how came if I don’t pass both name and days to the logTime function, the name global variable override a variable that its name is not name also?

just want to check my understanding with the variable concept and prevent the same mistake in the future.

Hi mathsap,

in the functions
logEvent(name, event);
logTime(name, days);
you pass 2 arguments while the functions definition uses only one. Therefore when called only “name” parameter is used as the first one.
When you call the functions you don’t have pass “name” because it’s a global variable.

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