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}.
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).
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.
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 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!