Magic 8 Ball Switch Statement

Hello,

I was looking at all the other solution of the magic 8-ball but don’t understand where I went wrong. 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;
8: eightBall =‘Signs point to yes’; break;

Hello! Which programming language are you working with?
I can see there is the word”case” missing in the last line before 8. Also no closing brace”}” at the end.

1 Like

That looks very much like JavaScript, which if I’m not mistaken is the only course track with the M8B project/exercise.

1 Like

I thought I’d done it on some other tracks, but can’t remember clearly :sweat_smile:

There are so many languages that look alike it can only be truly determined by the given evidence. The solution you offered was the best fit.

Even after putting in the case behind the 8 and the } at the end it still wouldn’t work.

Please post all of the code you have, including before and after the above. Paste it into a reply, then select all of it and click the </> button in the toolbar to format the code.

1 Like

This is my code before
let userName = ‘’
userName ? console.log(Hello,${userName}!) : console.log(‘Hello!’);
const userQuestion = ‘Do you like men’;
console.log(userQuestion);
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;
8: eightBall =‘Signs point to yes’;
break;

console.log(eightBall)
console.log(eightBall)

This is my code after:
let userName = ‘’
userName ? console.log(Hello,${userName}!) : console.log(‘Hello!’);
const userQuestion = ‘Do you like men’;
console.log(userQuestion);
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;
}
console.log(eightBall)
console.log(eightBall)

If we’re going to be able to communicate, it has to begin with formatted code samples. We suggested one method of posting code, now perhaps you might segue to the new user section and read up on posting code in the forums. Please post formatted code so the forum software doesn’t make it unusable to us without extensive editing to get out all the stylized quotes, etc. We cannot be of much help if the code is unusable to us. By ‘unusable’ I mean untestable.

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())));

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.