Let’s format that and comment a bit:
let userName = '';
// NO!
// userName ? console.log(Hello,${userName}!) : console.log('Hello!');
console.log(`Hello${userName ? `,${userName}` : ''}!`);
const userQuestion = 'Do you like men';
console.log(userQuestion);
// this will give you 0..7
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = '';
switch (randomNumber) {
case 1: eightBall = 'It is certain';
break;
case 2: eightBall = 'It is decidely so';
break;
case 3: eightBall = 'Reply hazy try again';
break;
case 4: eightBall = 'Cannot predict now';
break;
case 5: eightBall = 'Do not count on it';
break;
case 6: eightBall = 'My sources say no';
break;
case 7: eightBall = 'Outlook not so good';
break;
case 8: eightBall = 'Signs point to yes';
break;
default: // this will show you if you've screwed up
eightBall = `bad value: ${randomNumber}`;
}
console.log(eightBall);
// this makes not sense, you ain't getting a different result console.log(eightBall);
Ok, let’s make a function for that random number and test it out:
// a function to get the random number we're after
const getRandEightballNum = () => Math.floor(Math.random() * 8);
// let's test that puppy, show me 30 tests
console.log(new Array(30).fill(0).map(getRandEightballNum));
[
4, 5, 0, 3, 0, 3, 3, 7, 2,
6, 5, 6, 6, 3, 3, 6, 4, 5,
7, 2, 3, 4, 2, 1, 1, 3, 0,
3, 5, 0
]
Hmm… we weren’t expected 0s and no 8s. There is an obvious fix to this, which I’ll leave it to you.
Now, once you’re implemented that, make another function:
const getRandEightballNum = () => // your fixed result here
// function that takes a number and returns a message
const getEightballMessage = eightBallNum => {
let eightBall = `bad value: ${eightBallNum}`;
switch (eightBallNum) {
case 1: eightBall = 'It is certain';
break;
case 2: eightBall = 'It is decidely so';
break;
case 3: eightBall = 'Reply hazy try again';
break;
case 4: eightBall = 'Cannot predict now';
break;
case 5: eightBall = 'Do not count on it';
break;
case 6: eightBall = 'My sources say no';
break;
case 7: eightBall = 'Outlook not so good';
break;
case 8: eightBall = 'Signs point to yes';
break;
}
return eightBall;
};
// test that a few times
new Array(30).fill(0).forEach(() => console.log(getEightballMessage(getRandEightballNum())));