so I am having trouble with it printing the specific name that I type in the userName section. I want it to print Oscar if I type Oscar and Jane if I type Jane but I don’t know how to get it to do that do you think you could be of some assistance
Hi there! Could you copy and paste your code to the forum here? That would make it easier to see what is happening with your code.
ok I will thx for the tip wait one sec
let userName = ‘’;
userName ? console.log(‘Hello, Jane’) : console.log(‘Hello!’);
(not code after this)
this is all I have gotten done so far but I am stuck
I want it to print the username I type into the userName variable and not only print Jane
Also, could you paste a link to the lesson you are working on? That way we can see exactly what the lesson intends for you to do.
Hey @grizzlyslayer, after you get unstuck here, please take a look at this article: How to ask good questions (and get good answers). It’ll help for next time.
and @abbrendle thanks for being so helpful!
Hello grizzlyslayer!
If this is all the code you have:
let userName = ‘’;
userName ? console.log(‘Hello, Jane’) : console.log(‘Hello!’);
Then I suspect the issue you are having is with the console.log('Hello, Jane')
part.
Remember that in order to use the data stored in a variable, you need to use that variable name. So for your case, it would be:
let userName = ‘’;
userName ? console.log(‘Hello, ’+userName) : console.log(‘Hello!’);
And finally, if you want Oscar to print out, then you’d need to define let userName = 'Oscar';
Do you see the difference? Hope that helped!
hello thx so much for trying to help me I ended up looking how to do something and my dad helped too and my problem was fixed! thx everyone!
Can I jump in on this thread? I completed the project doing the switch method, but I’m having trouble with the If else method…?
let userName = (‘Bec’)
userName ? console.log(Hello ${userName}
) : console.log(‘Hello’);
let userQuestion = ‘Can you answer my question?’;
console.log(The user asked: ${userQuestion}
);
let randomNumber = Math.floor(Math.random() * 8);
let eightBall = ‘’;
if(randomNumber = 0) console.log (‘It is certain’)
{
}else if(randomNumber = 1) console.log (‘It is decidedly so’); {
}else if(randomNumber = 2) console.log (‘Reply hazy try again’); {
}else if(randomNumber = 3) console.log (‘Cannot predict now’); {
}else if (randomNumber = 4) console.log (‘Do not count on it’); {
}else if (randomNumber = 5) console.log (‘My sources say no’); {
}else if (randomNumber = 6) console.log (‘Outlook not so good’); {
}else (randomNumber = 7) console.log (‘Signs point to yes’);
console.log (The eight ball answered: ${eightBall}
)
Hi @rebeccaredfern, first let me share this with you, it will be useful in the future! How Do I Format Code In My Posts?
About your problem, you are using single =
symbols rather than ==
or ===
(that are used for comparison!)
ah thankyou so much!
im not sure if you are doing your code right
just looked it up because I forgot too
else if(randomNumber = 1) {console.log (‘It is decidedly so’)
}
that is the correct code
Hi… I just finished the Magic 8 Ball project in Javascript. I’m just wondering about the suggested code below:
let userName= ''
userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
let userQuestion= ''
userName ? console.log(`${userName} asked ${userQuestion}`) : console.log(`The user asked: ${userQuestion}`);
const randomNumber= Math.floor(Math.random()*8);
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;
}
console.log(eightBall);
In the above code, we need to save each response to eightBall variable according to the randomNumber first. Why we need to do that? Why don’t we just write like so:
let userName= ''
userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
let userQuestion= ''
userName ? console.log(`${userName} asked ${userQuestion}`) : console.log(`The user asked: ${userQuestion}`);
const randomNumber= Math.floor(Math.random()*8);
switch (randomNumber) {
case 0 :
console.log('It is certain');
break;
case 1 :
console.log('It is decidedly so');
break;
case 2 :
console.log('Reply hazy try again');
break;
case 3 :
console.log('Cannot predict now');
break;
case 4 :
console.log('Do not count on it');
break;
case 5 :
console.log('My sources say no');
break;
case 6 :
console.log('Outlook not so good');
break;
case 7 :
console.log('Signs point to yes');
break;
}
Thanks for anyone responding my question.
it’s a design choice. The end result is the same, as you very well noticed. It’s just that the person who wrote this lesson thought of it this way. If you want to think deeper about it, it’s easy to imagine that by storing the text in a variable and then using console.log()
, we have access to that answer to do other things with it. It would be more information available to the rest of the code than your version of directly printing the strings.
It’s not better or worse, really. It comes down to what you need your code to do and the many ways to go about it (the beauty of programming!)
Oh thanks! I got it. The first version of the code will be useful when we want to do something more with the eightBall variable, right?
Right. In theory, by having the result stored in a variable, you can access that information later and do more with it. Of course this is a small lesson and that won’t happen, but you can see the spirit of the choice.