trainingDays.js : bug?

Concerning trainingDays.js exercise.

Did what the YouTube ‘teacher’ said, but syntax error stays.
Can anybody help, because stuck … and in 2 day’s the free PRO program stops.
Thanks. Francis, Belgium.

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

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

Hello @cuigniez, welcome to the forums! Could you post your code by clicking this button:


And inserting it between the back ticks:
Screen Shot 2020-05-02 at 14.06.13
Also, could you post a link to the exercise, and the error message, please?

Dear Codeneutrino,

Thanks for helping me out.

Sorry for errors, because first time I use this channel (I’me 64 … but enthusiastic).

All best
Francis
Gent, Belgium


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

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

Dear all,

It is a bit unclear how to ‘post’ an error.
I am new to this way of working.
Can anybody explain how to do this, without being ridiculous?

Thank you

Hello,

I’d just like to say that you’ll never come across as ridiculous for not knowing something, asking questions, making mistakes, and the like. It’s part of the learning process.

I think @codeneutrino simply asked for the error message that you received back from the console. When running your code, the Codecademy console should have returned an error, as you implied in your first post:

Now, looking at your code, I noticed something on two different lines:

const logEvent (name,event) => {

and

const logTime (name,days) => {

Would you say that these two lines look right? Or is there something missing?

2 Likes

Dear ghostlovescore,

Well, still puzzled.
Cannot see where I got it wrong.
Bit frustrating.
Also I notice that the parameter ‘event’ stains blue, and name is ‘orange’.
All best

Francis
(PS: new to posting things, I may be doing strange things …)

/home/ccuser/workspace/learn-javascript_scope-training-days-ii/trainingDays.js:31
const logEvent(name,event) => {
      ^^^^^^^^
SyntaxError: Missing initializer in const declaration
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    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)
    at startup (bootstrap_node.js:151:9)

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


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


Hello @cuigniez. When you define a function using arrow-syntax, it looks like this:

const someFunction = (para1, para2) =>{
//some code
} ;

Does that syntax look the same as the way you are creating the arrow functions?

1 Like

To this I’ll add a little tip, @cuigniez


We got a SyntaxError, and that’s great to figure out what went wrong!

Whenever this comes up, the best thing to do is to simply look it up, if the error itself isn’t helpful enough.

If you google “SyntaxError: Missing initializer in const declaration

This comes up:

SyntaxError: Const must be initalized (Edge)
SyntaxError: missing = in const declaration (Firefox)
SyntaxError: Missing initializer in const declaration (Chrome)

The Firefox message might be a little bit more obvious.

Anyway, don’t hesitate to look things up this way. It’s a great way to not only fix your syntax errors, but also learn new things in the process.

Hope this helps

2 Likes

Thank you, made my day.
How could I not have seen that?
Thanks for advises. Will be more careful next time.
Now I can confirm the PRO program.
All best

1 Like