Javascript - Project 1 conditional statements


#1

Dear all of you ,

I have solved the first project of Javascript related to the section Conditional Statements.

let userName = 'Alessio';
if (userName){
  console.log('Hello, ' + userName + '!');
} else {
  console.log('Hello!');
}

let userQuestion = 'What is the capital city of Italy?';
console.log(userQuestion);

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

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

The instructor uses the variable eightball to save the string of case statement. I haven’t used it. The result seems to be the same.

What do you think? Which is the difference in these two types of approach?

Thank you for your help

Best Regards
Alessio


#2

Some ways are more effiecient than others when coding. The instructor may also be trying to get you to learn the concept of why that project is there.

I hope this helps =)


#3

Using a variable lets the reader identify the object and what it will contain, not just how it will behave.

eightball = 'Cannot predict now';

Both forms are repetitive, eightball vs. console.log(). For my own part I would rather log once, not in multiple branches.

console.log(eightball)

When you get into functions this will make more sense. For now, write what works, and give your due diligence in review to study, refine and simplify. Work toward a consistent style. This only comes with practice and time.


#4

Thanks for your help!