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:
-
If
let randomNumber = Math.floor(Math.random() * 8);
is yielding values among0*8
and1*8
, excluding 8 (so it’s actually getting values among 0 and 7), do I need adefault
clause within myswitch
statement? — “clause” and “statement” in italics because I don’t know if it’s the proper semantics -
I’ve not used any
const
variable… is that OK? I’ve let myself be carried away by the use oflet
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 amonglet
andconst
? -
In this case I’ve declared
eightBall
as variable and I’ve equated it to a different string for eachcase
of myswitch
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
-
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?
-
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.