Getting 'undefined' instead of the user name

var userName = 'Walid';

userName = true ? console.log(`Hello, ${userName}!`) :
console.log('Hello!');

var userQuestion = 'will I get a job this year';
console.log(userName + ':' +userQuestion + '?');

var randomNumber = Math.floor(Math.random() * 8);

var eightBall = '';

switch (randomNumber) {
  case 0 :
    console.log('It is certain');
    break;
  case 1 :
    console.log('It is decidedly so');
    break;
  case 2 :
    console.log('Reply hazy try again');
    break;
  case 3 :
    console.log('Cannot predict now');
    break;
  case 4 :
    console.log('Do not count on it');
    break;
  case 5 :
    console.log('My sources say no');
    break;
  case 6 :
    console.log('Outlook not so good');
    break;
  case 7 :
    console.log('Signs point to yes');
    break;
}

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-control-flow/projects/magic-eight-ball-1

Because here:

userName = true ? console.log(`Hello, ${userName}!`) :
console.log('Hello!');

you made a slight mistake. true is your condition, so the variable (userName) gets assigned the result of .log() method. But .log() doesn’t return anything.

3 Likes

thanks a lot!
I replaced the snippet you mentioned with :

userName === true ? console.log(`Hello, ${userName}!`) :
console.log('Hello!');

and it worked!

i would still go for:

console.log(condition ? true : false)

Then you don’t have to type console.log twice, and reducing change of errors like you just maybe (possible)

1 Like

can you show me please how can I implement your suggestion in my code?

No, i want you to make that leap yourself. so true and false are replaced by the strings you want to log. And what is your condition?

1 Like

i think the condition here is the username has to be true , so it would be like this:

console.log(userName ?  `Hello, ${userName}!` : 'Hello!' );
1 Like