Magic Eight ball Help

Okay so I changed the switch (eightBall) to switch (randomNumber) but when i did this it gave me a error.

Also the purpose of 'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');
Was in step 2 it says : create a ternary expression that decides what to do if the user enters a name or not. If the user enters a name — like 'Jane' — use string interpolation to log Hello, Jane! to the console. Otherwise, simply log Hello!

Let = ' ' Was suppose to be Let eightBall = ' '

I put

var eightBall = ' '
console.log(eightBall);

Because without it it was returning with error.

Okay so I changed the switch (eightBall) to switch (randomNumber) but when i did this it gave me a error.

What was the error message?


As I pointed out before, this code:

'Jane' ? console.log('Hello, Jane!') :
console.log('Hello!');

is in terms of functioning equal to this code:

if ('Jane') {
    console.log('Hello, Jane!')
} else {
    console.log('Hello!');
}

and because 'Jane' is not an empty string this code will work in the same way as this one:

if (true) {
    console.log('Hello, Jane!')
} else {
    console.log('Hello!');
}

do you see the problem? It will always print out 'Hello, Jane!', no matter what is the value of userName.


Because without it it was returning with error.

That is not a good reason :slight_smile: Just delete this line and tell us what is the error message.

Alright heres the error msg

/home/ccuser/workspace/learn-javascript-U2P1/main.js:22
    randomNumber = 'Do not count on it'
                 ^

TypeError: Assignment to constant variable.
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-U2P1/main.js:22:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

Ok, so I am kind of lost.

In the switch statement we want to assign random answer to the variable eightBall, right? randomNumber has a random value from 0 to 7. If randomNumber has value 0 we want to assign "It is certain" to the eightBall, if value is 1 then eightBall should have value "It is decidedly so" etc.

So why you change the value of randomNumber here:

randomNumber = 'Do not count on it'

? This value should be assigned to the eightBall variable.


In the switch we want to code the logic that sets value of eightBall based upon random value of randomNumber.

In your first reply you said
factoradic:

Ok, problem is in the switch value of eightBall should be dependent on the randomNumber value. So this switch (eightBall) should be changed to switch (randomNumber).

So I did as you said and changed it

Edit: Also when I run it it does pick a random number and it changes the randomNumber = 'Do not count on it' to the other numbers

Ok, if we take your code from the first post:

switch (eightBall) {
  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 = 'Signs point to yes'
    break;
}

and if we change switch (eightBall) to switch (randomNumber) we should get this:

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 = 'Signs point to yes'
    break;
}

The error is gone because we do not try to change the value of constant (randomNumber).


By looking at this discussion I think that problem is not with the syntax, but with understanding of the switch. It might be a good idea to revisit lessons about switch.

Oh, Sorry about that I just tried it the problem was I thought u meant to put the “randomNumber” in all of the switch (all of the cases) my mistake for interpreting it wrong. Also if could could tell me why when I run it it says
Hello, Kyle! Am i good at nitrotype?: Am i good at nitrotype? The eight ball answered: It is decidedly so
And that will be all that I need I think i just have to take out the
console.log(`Am i good at nitrotype?: ${userQuestion}`);
But I may be wrong.

Other than that thank you for your help all day I have been trying to fix this throughout my classrooms and nobody I asked knew and there was a video on how to do it but my school does has blocked it and wont let me watch it. Otherwise I would’ve just watched it instead of wasting your time. Thank you :slight_smile:

1 Like

Otherwise I would’ve just watched it instead of wasting your time.

If you learnt something or at least if my comments were slightly helpful then it wasn’t a waste of time :slight_smile:


You are right, this part of output:

Am i good at nitrotype?: Am i good at nitrotype?

is the result of this line of code:

console.log(`Am i good at nitrotype?: ${userQuestion}`);

This is the code after the changes proposed in my first comment:

var userName = 'Jane';

userName === 'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');

var userQuestion = 'Am i good at nitrotype?';

console.log(`${userQuestion}`);

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

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 = 'Signs point to yes'
    break;
}

console.log(`The eight ball answered:
 ${eightBall}`);
1 Like

Hello everyone! While playing with the code I came up for this exercise, I found out the console is printing undefined after my greeting. Does anyone know why this is happening? Attached is my code:

let userName = 'Joe';
function greeting (userName){
  if(userName !== ''){
    console.log('Hello '+ userName + '!');
  } else {
    console.log('Hello!')
  }
};
console.log(greeting(userName));
let userQuestion = 'Will I win the lottery this week?';
console.log( userName + ' wants to ask a question : ' + userQuestion);
let 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 = 'Signs point to yes';
    break;
};
console.log('The eight ball answered: ' + eightBall);

In this statement you call the greeting function. The function prints the greeting as specified inside of its code body. Since you don’t have a return statement in the function, the function implicitly returns 'undefined' to the caller. In this case the caller is console.log(), so 'undefined' gets logged to the console after your greeting. To fix this behavior you can do one of two things. Either change your console.log() statements inside your function to return the greeting, or just invoke the function without the console.log() like so: greeting(userName).

let userName = 'Joe';
function greeting (userName){
  if(userName !== ''){
    console.log('Hello '+ userName + '!'); //change console.log to: return
  } else {
    console.log('Hello!') //same thing here
  }
};
console.log(greeting(userName)); //Or change to: greeting(userName);

Thanks! I just fixed it

1 Like

Hello! Struggled through this and got this as my answer but the code won’t run. Can anyone tell me where i’ve gone wrong?

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

let userQuestion = 'Will I ever learn JavaScript?';
console.log(`${userName} asked: ${userQuestion}`);

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

let eightBall ='';

if (randomNumber === 0){
  eightBall = 'It is certain';
} else if (randomNumber === 1){
  eightBall = 'It is decidedly so';
} else if (randomNumber === 2){
  eightBall = 'Reply hazy try again';
} else if (randomNumber === 3){
  eightBall = 'Cannot predict now';
} else if (randomNumber === 4){
  eightBall = 'Do not count on it';
} else if (randomNumber === 5){
  eightBall = 'My sources say no';
} else if (randomNumber === 6){
  eightBall = 'Outlook not so good';
} else (randomNumber === 7){
  eightBall = 'Signs point to yes';
}

console.log(`${eightBall}`);

This is the only thing preventing your code from running. The else code block executes as a default if no prior if or else if conditions are truthy. You cannot test a condition in your else block. If randomNumber != to 0 through 6 the only other possible value is 7. Happy Coding!

1 Like

Hi there! i managed to make the control flow work with the switch statements but when I use the if / else if the eight ball answer comes before the string:

Hello Martina!
The user asked
Cannot predict now
The eight ball answered:

Here’s my code:

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

let eightBall = ''; 

if (randomNumber === 0) {
  console.log('It is certain');
}
else if (randomNumber === 1) {
  console.log('It is decidedly so');
}
else if (randomNumber === 2) {
  console.log('Reply Hazy try again');
}
else if (randomNumber === 3) {
  console.log('Cannot predict now');
}
else if (randomNumber === 4) {
  console.log('Do not count on it');
}
else if (randomNumber === 5) {
  console.log('My sources say no');
}
else if (randomNumber === 6) {
  console.log('Outlook not so good');
}
else if (randomNumber === 7) {
  console.log('Signs point to yes');
  
  
}

console.log(`The eight ball answered: ${eightBall}`);

Not sure what I’m doing wrong??

In your code, you declaring eightBall as an empty string. When you determine what the answer will be with your if…else if statements, you are logging the answer to the console. This happens before your

console.log(`The eight ball answered: ${eightBall}`);

statement. When this statement finally executes, eightBall is still empty. If you change console.log('answer'); to eightBall = 'answer'; you will get the outcome you were expecting.
Also, just a side note, your final else if only needs to be an else with no condition to test. If random number wasn’t 0 - 6, 7 is the only value remaining that it could be, so else will suffice.
Happy coding!

1 Like

This worked for me on Step 9.

  • If you started with a switch statement, convert it to if/else if/else statements. -

I tried over and over again and finally used the ==.
Here’s my code below!

let userName = ‘Nancy’;

userName ? console.log(Hello ${userName}!) : console.log(‘Hello!’);

const userQuestion = ‘Will I become a mermaid this summer?’;
console.log(The user asked: ${userQuestion});
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = ‘’;

if (randomNumber == 0) {
console.log(‘The eight ball answered: It is certain’);
} else if (randomNumber == 1) {
console.log(‘The eight ball answered: It is decidedly so’);
} else if (randomNumber == 2) {
console.log(‘The eight ball answered: Reply hazy try again’);
} else if (randomNumber == 3) {
console.log(‘The eight ball answered: Cannot predict now’);
} else if (randomNumber == 4) {
console.log(‘The eight ball answered: Do not count on it’);
} else if (randomNumber == 5) {
console.log(‘The eight ball answered: My sources say no’);
} else if (randomNumber === 6) {
console.log(‘The eight ball answered: Outlook not so good’);
} else if (randomNumber == 7) {
console.log(‘The eight ball answered: Signs point to yes’);
} else {
console.log(‘The eight ball is broken’)
};

So the code works perfectly without

let eightBall = '';

Can you tell me why the exercise called for this in the first place?

What code works perfectly without let eightBall = '';? Could you share the code in question? The declaration of the variable eightBall has a specific purpose the way it is used in the lesson. If you are using either a switch ... case block, or an if ... else if ... else block to assign a value to eightBall like so; eightBall = 'It is decidedly so.', without the aforementioned variable declaration, you end up declaring eightBall as a global variable at the time of assignment. In a small project like this, there is little to no consequence, but it is very bad practice to give any variable scope beyond it’s needs. It can lead to many problems in a large more complicated project. By using let eightBall = '' prior to assigning a value later, you are limiting the scope to the block of code in which it was declared including any nested code blocks.

3 Likes

The code that user factoradic posted. Here’s mine for good measure.

let userName = 'Buzz';

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

let userQuestion = 'Will I ever return to the moon?';

console.log(`${userQuestion}`);

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

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 = 'Signs point to yes'
    break;
}

console.log(`The eight ball answered:
${eightBall}`);

Your point on the global scope of eightBall is well taken, thank you. I understand why the exercise didn’t want to overload us with concepts we may not be familiar with yet, but it’s still good to know why a piece of code is necessary.

1 Like

I thought that if we wanted to always print our ‘Hello, Jane!’, it was needed to use: var userName = '' userName ? console.log('Hello, Jane!') : console.log('Hello!');

instead of : 'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');