Can I console.log the result of a switch statement?

[https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-conditionals/projects/magic-eight-ball-1]

In another helpful post, I saw an awesome way to refactor console log instructions for concise ternary operators i.e.

let yes = true
console.log(yes ? 'Oui!" : "Non!")
// outputs 'Oui!'

instead of

yes ? console.log('Oui!') : console.log('Non!')

For the magic eight ball mini-project, I was wondering if I could do the same thing for an entire switch statement. That way, I could factor out the repeated “console.log()” calls.

An example of what I’m trying to do, that isn’t working:

let number = 1
console.log(switch (number) {
    case 0:
        'Hi!'
        break;
    case 1 :
        'Bye!'
        break;
    default:
        'Whatever.'
        break;
})

Is this simply not something I can do or am I coding it wrong?

1 Like

Printing once directly from a switch statement is not something you can directly do I don’t believe, no (although more than happy to be corrected there if I’m wrong!). You also of course could just chain a whole load of ternary statements together, that would be very spagetti-esque and inefficient so probably not ideal.

One other option would be to have the switch inside of a function, return the values and print the result of the function call, or to assign the string to a variable and just print the variable once at the end.

That said, I don’t personally think there’s a huge issue with having multiple console.logs, particularly for something like the Magic 8-Ball project where the whole idea is to print a different value based off the results of a switch statement

1 Like

So something like?

let magicEightBall = () => {
    switch (randomNumber) {
        case 0:
            return 'Blah'
            break;
        etc.

 console.log(magicEightBall)
1 Like

Pretty much yeah! Although you need to call the function when printing:

// This calls it
console.log(magicEightBall())

// This does not
 console.log(magicEightBall)

Ok got ya. That’s sad I can’t print it directly within the console.log() call, but then again, defining a separate function to accomplish what I’m doing is probably better coding practice.

1 Like

Defiantly yeah, using functions for small reusable bits of code is super useful. Glad I could help! :smile:

If you’re still around, could you see the error here? I’m getting an “unexpected token switch” error:

let eightBall = () => switch (randomNumber) {
  case 0:
    return 'It is certain'
    break;
  case 1:
    return 'It is certain'
    break;
  case 2:
    return 'It is certain'
    break;
  case 3:
    return 'It is certain'
    break;
  case 4:
    return 'It is certain'
    break;
  case 5:
    return 'It is certain'
    break;
  case 6:
    return 'It is certain'
    break;
  case 7:
    return 'It is certain'
    break;
  default:
    return 'I don\'t know'
}

console.log(eightBall())

You’re missing a pair of curly brackets for the function (you only have the pair for the switch itself)

YAYYYYY!!! Thank you!!! The code, she is beautiful now!

1 Like

Awesome! Great job! :slightly_smiling_face: