Javascript magic 8 ball challenge

I am working on the magic 8 ball javascript assignment and this is my code:
//sample

let userName = ‘user’;
userName ? console.log(’ Hello, ${userName}’) :
console.log(‘Hello’);
let userQuestion = ’ some question’;
console.log(’${userName} has asked - ${userQuestion}’)

// the problem that I am having is getting the variable to print inside the terminal
//it comes out as
Hello, ${userName}

//the other comes out as
${userName} has asked - ${userName}
//any reason why?? sorry i am a noob :frowning:

Hey @darth_necro welcome to the forums.

Do you see anything wrong with what marks you use in the console.log?

And don’t forget to format your code [How to] Format code in posts

I’m not sure, would it be because the placement of the $ is wrong? I’m also thinking that I might be using the {} instead of ?? Other than that i’m not sure my friend…

When you doing the String Interpolation with ${userQuestion} that’s fine. It’s what you use to surround the string. You have 3 choices ', ", and `. Only one will work for the value of the variable will go in place of the ${userQuestion}.

Hi, Sorry I didn’t know where I can go to post my own question, but can anyone help me figure out why my code isn’t working. Please and thank you!

let userName = ‘David’;

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

console.log(‘Hello!’);

let userQuestion = ‘Will I get a puppy soon?’;

console.log(’${userName} has asked - ${userQuestion}’);

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

console.log(‘randomNumber’);

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;

case 8:
eightBall = ‘Of course you are’;
break;

console.log(‘The Magic 8 Ball says, ${eightBall}.’);
}

1 Like

I think the
console.log(`The Magic 8 Ball says, ${eightBall}.`);
should be after the }
instead of before it (so that it is not inside the switch statement).

I will try that… Thanks for replying back!

Hey guys sorry I havent responded in a week, got kinda busy lol.
So my code works but the final output is not coming out this is what I have…

let userName = ’ kreg’;
userName ? console.log(Hello,${userName}): console.log(‘Hello’);

let = userQuestion = ‘Will I eat again later?’;
console.log(${userName} has asked - ${userQuestion});
let randomNumber = Math.floor(Math.random() * 8);

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 ball says ${eightball});

}

the last console.log isnt printing to the console for some reason. :frowning:

that last console.log( ) should be outside the switch statement block (meaning after the } instead of before it).

The switch statement is definitely something we should get some practice with, but once we’ve done that we should also be looking for alternatives, if logic permits. Eight Ball permits alternatives, not the least is an array. Random values within a range can act as indices.

const eightball = function () {
  return [
    'It is certain',
    'It is decidedly so',
    'Reply hazy try again',
    'Cannot predict now',
    'Do not count on it',
    'My sources say no',
    'Outlook not so good',
    'Signs point to yes'
  ][Math.floor(Math.random() * 8)]
}

A switch is very useful when there are a range of functions to be carried out, but when it is just data, as demonstrated above, work with that.

It is especially important to note that functions are expressions, and arrays contain expressions. Therefore arrays can contain functions, especially when written as expressions, namely arrow functions.

{
const mathFunction = function () {
  return [
    x => x + 2,
    x => x - 2,
    x => x * 2,
    x => x / 2
  ][Math.floor(Math.random() * 4)]
}
console.log(mathFunction()(10))
}
20
undefined
{
const mathFunction = function () {
  return [
    x => x + 2,
    x => x - 2,
    x => x * 2,
    x => x / 2
  ][Math.floor(Math.random() * 4)]
}
console.log(mathFunction()(10))
}
5
undefined
{
const mathFunction = function () {
  return [
    x => x + 2,
    x => x - 2,
    x => x * 2,
    x => x / 2
  ][Math.floor(Math.random() * 4)]
}
console.log(mathFunction()(10))
}
12
undefined
{
const mathFunction = function () {
  return [
    x => x + 2,
    x => x - 2,
    x => x * 2,
    x => x / 2
  ][Math.floor(Math.random() * 4)]
}
console.log(mathFunction()(10))
}
8
undefined

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

const userQuestion = ‘What is Magic Eight Ball?’;
console.log(${userName} asked: ${userQuestion});

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

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;
default:
console.log(There appears to be a problem ${userName}! Please contact nine bal services.);
break;
}

console.log(Magic Eightball: ${eightBall});

//I only built part of the code, the rest was with help. Thanks for welcomen!