Magic Eight Ball project

Hello. I just start learning and decided to add some functions to Eight Ball project.
Can you advise me if there is anything I can do to make the code better? Maybe make it simpler somehow.

let userName = 'stan';

// Check if userName contain numbers
function hasNumber(userName) {
var regex = /\d/g;
return regex.test(userName);
if (hasNumber(userName) === true || typeof userName == 'number') {
  console.log('Name can not contain numbers!')
  return false;
} else {
  //Function to capitalize first letter in userName
  function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
userName = capitalizeFirstLetter(userName);
userName ? console.log(`Hello, ${userName}!`) : console.log(`Hello, Stranger!`);
/*userName ? console.log(`Hello, ${userName}!`) : console.log(`Hello, Stranger!`);*/
const userQuestion = `Nice day for fishing ain't it?`;
userName ? console.log(`${userName} asked: "${userQuestion}"`) : console.log(`Stranger asked: "${userQuestion}"`);
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = '';
switch (randomNumber) {
  case 0:
  eightBall = 'Well, I think so.';
  case 1:
  eightBall = 'It is certain';
  case 2:
  eightBall = 'It is decidedly so';
  case 3:
  eightBall = 'Reply hazy try again';
  case 4:
  eightBall = 'Cannot predict now';
  case 5:
  eightBall = 'Do not count on it';
  case 6:
  eightBall = 'My sources say no';
  case 7:
  eightBall = 'Outlook not so good';
  case 8:
  eightBall = 'Signs point to yes';
console.log(`The eight ball answered: "${eightBall}"`); 

Thanks in advance and sorry for my English :slight_smile:

Hi @ammigo887874160927 and welcome to the forum!
Nice work adding the extras.
Have you tried to run this code yet? Because the way you posted it here would produce errors. So you would have to debug it before you start thinking about potential improvements. Or did you just post a part of your code?
For now, there is no scope for all your if and else statements as well as your switch cases. You’d have to wrap them in a function in order for them to work.

1 Like

Hi @mirja_t . Hm, strange. It works in CodeAcademy simulator, but in Chrome DevTools produce errors.
I’ll try to figure out with errors and fix them, but later.