Regarding Magic Eight Ball practice: code differences

There being some 60+ open threads regarding the Magic Eight Ball practice, I feel somewhat awkward opening a new one, but in the few threads I’ve opened, I’ve found no exact consultation to this one I am doing next:

Thing is that I’ve come up with some code which works exactly as it is requested by the exercise, but I’ve done it by not declaring a variable requested by the exercise. It can be seen in the following two snippets of code:

First snippet

let userName = 'Jane'; // Variable "userName" declared. Users can or cannot say their name.

userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!'); // Ternary operator: if users say their name, "userName" exists, so console shows "Hello, userName!"; else, it shows just "Hello!"

let userQuestion = 'Is it gonna rain?'; // Variable "userQuestion" declared. Here, users ask the Magic Eight Ball their question.

userName ? console.log(`${userName}: ${userQuestion}`) : console.log(userQuestion); // Ternary operator: if users have said their name and stated their question, console will show up "userName: userQuestion?"; if there's no name or no question, they won't be displayed.

let randomNumber = Math.floor(Math.random() * 8); // Here, "randomNumber" is getting values among 0*8 and 1*8, excluding 8, so it's actually getting values among 0 and 7.

let eightBall = ''; // Declaring the variable "eightBall" so that it can be summoned later. 

switch (randomNumber) {
  case 0:
  eightBall = 'It is certain.';
  break;
  case 1:
  eightBall = 'It is decidely so.';
  break;
  case 2:
  eightBall = 'Reply hazy try again.';
  break;
  case 3:
  eightBall = 'Cannot predict now.';
  break;
  case 4:
  eightBall = 'Do not count on it.';
  break;
  case 5:
  eightBall = 'My sources says no.';
  break;
  case 6:
  eightBall = 'Outlook not so good.';
  break;
  case 7:
  eightBall = 'Signs point yes.';
  break;
  default: // Is this truly necessary, since I am just getting values 0 thru 7?
  console.log('Try again!');
}

console.log(eightBall);

This first code snippet is, I believe, the very exact thing that the exercise requests. My questions:

  1. If let randomNumber = Math.floor(Math.random() * 8); is yielding values among 0*8 and 1*8, excluding 8 (so it’s actually getting values among 0 and 7), do I need a default clause within my switch statement? — “clause” and “statement” in italics because I don’t know if it’s the proper semantics :sweat_smile:

  2. I’ve not used any const variable… is that OK? I’ve let myself be carried away by the use of let every time I have to declare a variable, but looking at the “hints” of the exercise, it feels now like a bad practice that I shouldn’t have acquired… Should I think carefully before deciding among let and const?

  3. In this case I’ve declared eightBall as variable and I’ve equated it to a different string for each case of my switch statement. Now, regarding this second snippet of code:

Second snippet

let userName = 'Jane';

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

let userQuestion = 'Is it gonna rain?';

userName ? console.log(`${userName}: ${userQuestion}`) : console.log(userQuestion);

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

switch (randomNumber) {
  case 0:
  console.log('It is certain.');
  break;
  case 1:
  console.log('It is decidely 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 says no.');
  break;
  case 6:
  console.log('Outlook not so good');
  break;
  case 7:
  console.log('Signs point yes');
  break;
  default: // Is it truly necessary, since I am just getting values 0 thru 7?
  console.log('Try again!');
}

I’ve not declared eightBall as a variable and I’ve introduced each possible answer from this proverbial Magic Eight Ball as a console.log(), so that when running the code the result is exactly the same. Obviously, I understand that the main difference about both of those code snippets is the having (or not) the possibility of calling upon the eightBall variable, but, once again, when running the code, the result is still the same.

Questions

  1. Is there something I should be deducing from this experience besides the fact that the same result can be achieved with two different pieces of code?

  2. Would there be a tangible difference for users when facing these two different snippets from “a front-end perspective”?

Thanks a lot in advance.

Cheers.

Hello, @ibaifernandez.

That is not exactly correct, but close. Math.random() returns a pseudo random float in the range (0 - <1). It could return 0.9999352537897834 (I ran Math.random() 10,000 times, and this was the largest value returned), but never 1.

No. You don’t. If the input was subject to including an ‘out of range’ value, you should have a ‘default’, but in this case, your code is controlling the value assigned to randomNumber, so there is no need for a ‘default’.

There are differing schools of thought on this one. Personally, I use const unless the value my variable is assigned to is going to change. I prefer to limit ambiguity as much as possible. When I go back over code I have written, and I see:

const myValue = 'blah';

I know that anywhere I see myValue it is still assigned to 'blah'. Versus:

let myValue = 'blah';

Now when I see, myValue later on in the code, I need to look for where the value may have changed.

For your remaining questions, there is no real difference in your 2 code snippets. It’s really your preference. Some might say you could ‘DRY’ your second snippet by not repeating console.log(). In your first snippet you could reduce the number of case's by one by initially assigning one of your responses to eightBall intead of an empty string, and then leaving that case out of the switch. For example:

As you continue to learn JavaScript, you’ll come across other ways to do this project. After you’ve learned about Arrays and/or Objects come back to this, and see how you could optimize this project even further. Happy coding! :slightly_smiling_face:

Edit: I just ran Math.random() an additional 1,000,000 times, and the largest value was 0.9999982347627723. Out of curiosity, I attempted to determine the largest possible value returned by Math.random(). The longest values have 18 digits to the right of the decimal. The highest value not rounded up to 1 by Math.floor() seems to be 0.999999999999999944.

> Math.floor(0.999999999999999944 * 1)
0
> Math.floor(0.999999999999999945 * 1)
1