Help with javascript 8 ball project


#1

https://www.codecademy.com/courses/learn-javascript/projects/magic-eight-ball

Hi, for some reason when I try to run this code in the codeacademy js window I don't seem to get a result, but if I open developer tools in chrome and run it, it seems to work. any help would be appreciated, thanks.

var randomNumber = Math.floor(Math.random() * 7);
var eightBall = '';
switch( Math.floor(Math.random() * 7)) {
  case 0:
    eightBall = 'It is certain';
    break;
  case 1:
    eightBall = 'It is decidedly so';
    break;
  case 2:
    eightBall = 'Cannot predict now';
    break;
  case 3:
    eightBall = 'Don\'t count on it';
    break;
  case 4:
    eightBall = 'My sources say no';
    break;
  case 5:
    eightBall = 'Outlook not so good';
    break;
  case 6:
    eightBall = 'Signs point to yes';
    break;
  case 7:
    eightBall = 'You are in grave danger';
    break;
  default:
    eightBall = 'Reply hazy try again';
   break;
    
 }
console.log('The user asked: ' + userQuestion);
console.log('The eight ball answered: ' + eightBall);

#2

This looks odd. Did you mean to write,

switch (randomNumber) {

I'm guessing you have a user input prompt for userQuestion?


#3

Yeah I meant to write switch (randomNumber) {
No I don't have a user input prompt, each time it runs it should just display a different response since it's using a random number between 1 and 7


#4

Looks like 0 to 7, but that's fine. It's in the correct range.

If you have not defined userQuestion then define it.

var userQuestion = "Will I pass my Calculus exam, tomorrow?";

Otherwise, it will raise an error.


#5

Like this?

var userQuestion = "Will I pass my Calculus exam, tomorrow?";
var randomNumber = Math.floor(Math.random() * 7);
var eightBall = '';
switch (randomNumber) {
  case 0:
    eightBall = 'It is certain';
    break;
  case 1:
    eightBall = 'It is decidedly so';
    break;
  case 2:
    eightBall = 'Cannot predict now';
    break;
  case 3:
    eightBall = 'Don\'t count on it';
    break;
  case 4:
    eightBall = 'My sources say no';
    break;
  case 5:
    eightBall = 'Outlook not so good';
    break;
  case 6:
    eightBall = 'Signs point to yes';
    break;
  case 7:
    eightBall = 'You are in grave danger';
    break;
  default:
    eightBall = 'Reply hazy try again';
   break;
    
    }
console.log('The user asked: ' + userQuestion);
console.log('The eight ball answered: ' + eightBall);

#6

I still get nothing when I try to run it


#7

I tested your code and got all the possible outcomes (with one minor tweak... should be * 8, not * 7).

var userQuestion = "Will I pass my Calculus exam, tomorrow?";
var randomNumber = Math.floor(Math.random() * 8);
var eightBall = '';
switch (randomNumber) {
  case 0:
    eightBall = 'It is certain';
    break;
  case 1:
    eightBall = 'It is decidedly so';
    break;
  case 2:
    eightBall = 'Cannot predict now';
    break;
  case 3:
    eightBall = 'Don\'t count on it';
    break;
  case 4:
    eightBall = 'My sources say no';
    break;
  case 5:
    eightBall = 'Outlook not so good';
    break;
  case 6:
    eightBall = 'Signs point to yes';
    break;
  case 7:
    eightBall = 'You are in grave danger';
    break;
}
console.log('The user asked: ' + userQuestion);
console.log('The eight ball answered: ' + eightBall);

Output when randomNumber is 0:

The user asked: Will I pass my Calculus exam, tomorrow?
The eight ball answered: It is certain

Also, since we are using a generated number in a fixed range, we do not need a default case in the switch.


#8

Thank you for your help I really appreciate it!


#9

Thanks, this helped me as well. My problem was that I put switch(eightBall) instead of (randomNumber).

Now corrected variables and switch:
var userQuestion = "Will tomorrow come?";
var randomNumber = Math.floor(Math.random() * 7);
var eightBall = '';
switch (randomNumber) {


#10

A post was split to a new topic: Why Save and not Run?


#11