userName output


#1

I declared userName = Ross

I wrote a condition if userName is true to print string Hello, ${userName}
if not print string Anonymous.

When I remove the userName variable and run the program, Hello still prints instead of Anonymous.

Any ideas where I’ve got it wrong?

userName = "Ross";

userName = true ? (`Hello ${userName}`)
: (`Anonymous`);

let userQuestion = `We were on a break, right?`;

const randomNumber = Math.floor(Math.random() * 8);
let eightBall = "";

switch(randomNumber) {
  case 0:
    eightBall = `It is certain`;
    break;
  case 1:
    eightBall = `It is decidedly so`;
    break;
  case 2:
    eightBall = `100% Yes`;
    break;
  case 3:
    eightBall = `Cannot predict now`;
    break;
  case 4:
    eightBall = `Count on it`;
    break;
  case 5:
    eightBall = `My sources say no`;
    break;
  case 6:
    eightBall = `Outlook looks so good`;
    break;
  case 7:
   eightBall = `Signs point to yes`;
    break;
  default:
    eightBall = `undefined`;
}

console.log(`${userName}, you asked: ${userQuestion}`);
console.log(`The eight ball answered: ${eightBall}.`);

Console log

With variable value Ross

Hello Ross, you asked: We were on a break, right?
The eight ball answered: My sources say no.

userName with empty string

Hello , you asked: We were on a break, right?
The eight ball answered: My sources say no.

#2

The code before the ternary operator ? needs to be a conditional. true is a boolean constant meaning always the same outcome.

userName is a string, “Ross”, not a boolean. We would not be testing for the true boolean, but for the truthiness.

An empty string is falsy, otherwise, truthy

userName ? expression : default;

Parens are not needed and should be left off for readability. Our eye interprets parens differently than just delimiters and they are superfluous in this case.

 > userName = ''
<- ""
 > console.log( userName === "" ? userName = `Anonymous` : `Hello ${userName}`);
   Anonymous
<- undefined
 > userName
<- "Anonymous"
 > userName = "Ross"
<- "Ross"
 > console.log(userName === "" ? userName = `Anonymous` : `Hello ${userName}`);
   Hello Ross
<- undefined
 > userName
<- "Ross"

Notice how the log method permits completion of an assignment within the output expression? Cool, eh?


Partially explained above, but let’s take a closer look at the overall statement.

userName = true ? `Hello ${userName}` : `Anonymous`;

The overall outcome will be an assignment to userName of either the Hello with the string value interpolated, or Anonymous.

Only problem is, as mentioned before, the boolean literal blocking one branch. The expression before the ternary operator (?) needs to be a conditional, not a constant.

The example above gets around this by permitting either an empty string or a valid string to enter the flow.

More importantly we do not alter the user name if it is already set, only if it is empty. That way the 'Hello ' portion of the string is not saved, only logged.

This will demonstrate how we can re-use the functionality…

 > const hello = () => console.log(userName === "" ? userName = `Anonymous` : `Hello ${userName}`);
<- undefined
 > userName = ""
<- ""
 > hello()
   Anonymous
<- undefined
 > userName = "Ross"
<- "Ross"
 > hello()
   Hello Ross
<- undefined

hello() is a global function accessing a global variable. It gives us re-use should we need it a second or subsequent time, such as above.


#3

@mtf Thanks for the detailed reply. I am working through your notes to better understand this exercise. Not going to lie, this all seems like a lesson in Chinese but I am determined to learn.