JavaScript Project

Hi everyone, I’m doing a javascript project but couldn’t find out where is wrong in this piece of code. The problem is the code couldn’t give the right answer for the variable “eightball” in each case both in switch statement and if statement. Could someone help me please? Thank you in advance!

let userName = ‘Ella’;
userName? (console.log(‘Hello, Jane!’)):(console.log(‘Hello!’))

let userQuestion = 'Could you tell me your number please? ';
console.log(userQuestion + userName);
let randomNumber = Math.floor(Math.random()*8);
console.log(randomNumber);

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

Hi @hanynguyen5714748474, I checked your code and made a few corrections. So first of all I used a simple if statement instead of the ternary operator, it’s a little longer but easier. I aslo used some template literals where needed.

I think most of your mistakes were in your switch case, the overall structure is good, but you don’t need to console.log after each statement and for your cases you don’t need to use single quotes for numbers.
Here is your code with a few corrections, you can compare it with yours and see what you might have missed.

let userName = 'Ella'; if (userName === 'Ella') { console.log('Hello, Ella!'); } else { console.log('Hello!'); } let userQuestion = 'Could you tell me your number please?'; console.log(`${userQuestion} ${userName}`); const randomNumber = Math.floor(Math.random()*8); let eightBall =''; switch (randomNumber) { case 0: eightBall = 'It is certain'; break; case 1: eightBall = 'It is decidedly 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 say no'; break; case 6: eightBall = 'Outlook not so good'; break; case 7: eightBall = 'You are not worthy'; break; case 8: eightBall = 'Signs point to yes'; break; } console.log(`${eightBall}`);

Hi @chrisgeek07!
Oh…Great! Thank you very much for showing me! I got it works.

1 Like

No worries, happy coding !