var userName = ''
'Jane' ? console.log('Hello, Jane!') :
console.log('Hello!');
var userQuestion = 'Am i good at nitrotype?'
console.log(`Am i good at nitrotype?: ${userQuestion}`);
const randomNumber = Math.floor(Math.random() * 8);
let = ''
switch (eightBall) {
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 eight ball answered:
${eightBall}`);
var eightBall = ''
console.log(eightBall);
This is the code I used for introduction to Javascript Magic eight ball when I run it it comes back with: âThe eight ball answered: Undefinedâ How do I get it working
Ok, problem is in the switch, value of eightBall should be dependent on the randomNumber value. So this switch (eightBall) should be changed to switch (randomNumber).
But there are more problems in your code, letâs tackle them together one by one.
Okay so I changed the switch (eightBall) to switch (randomNumber) but when i did this it gave me a error.
Also the purpose of 'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');
Was in step 2 it says : create a ternary expression that decides what to do if the user enters a name or not. If the user enters a name â like 'Jane' â use string interpolation to log Hello, Jane! to the console. Otherwise, simply log Hello!
/home/ccuser/workspace/learn-javascript-U2P1/main.js:22
randomNumber = 'Do not count on it'
^
TypeError: Assignment to constant variable.
at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-U2P1/main.js:22:18)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
In the switch statement we want to assign random answer to the variable eightBall, right? randomNumber has a random value from 0 to 7. If randomNumber has value 0 we want to assign "It is certain" to the eightBall, if value is 1 then eightBall should have value "It is decidedly so" etc.
So why you change the value of randomNumber here:
randomNumber = 'Do not count on it'
? This value should be assigned to the eightBall variable.
In the switch we want to code the logic that sets value of eightBall based upon random value of randomNumber.
Ok, problem is in the switch value of eightBall should be dependent on the randomNumber value. So this switch (eightBall) should be changed to switch (randomNumber).
So I did as you said and changed it
Edit: Also when I run it it does pick a random number and it changes the randomNumber = 'Do not count on it' to the other numbers
switch (eightBall) {
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;
}
and if we change switch (eightBall) to switch (randomNumber) we should get this:
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;
}
The error is gone because we do not try to change the value of constant (randomNumber).
By looking at this discussion I think that problem is not with the syntax, but with understanding of the switch. It might be a good idea to revisit lessons about switch.
Oh, Sorry about that I just tried it the problem was I thought u meant to put the ârandomNumberâ in all of the switch (all of the cases) my mistake for interpreting it wrong. Also if could could tell me why when I run it it says Hello, Kyle! Am i good at nitrotype?: Am i good at nitrotype? The eight ball answered: It is decidedly so
And that will be all that I need I think i just have to take out the console.log(`Am i good at nitrotype?: ${userQuestion}`);
But I may be wrong.
Other than that thank you for your help all day I have been trying to fix this throughout my classrooms and nobody I asked knew and there was a video on how to do it but my school does has blocked it and wont let me watch it. Otherwise I wouldâve just watched it instead of wasting your time. Thank you
Otherwise I wouldâve just watched it instead of wasting your time.
If you learnt something or at least if my comments were slightly helpful then it wasnât a waste of time
You are right, this part of output:
Am i good at nitrotype?: Am i good at nitrotype?
is the result of this line of code:
console.log(`Am i good at nitrotype?: ${userQuestion}`);
This is the code after the changes proposed in my first comment:
var userName = 'Jane';
userName === 'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');
var userQuestion = 'Am i good at nitrotype?';
console.log(`${userQuestion}`);
const randomNumber = Math.floor(Math.random() * 8);
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 eight ball answered:
${eightBall}`);
Hello everyone! While playing with the code I came up for this exercise, I found out the console is printing undefined after my greeting. Does anyone know why this is happening? Attached is my code:
let userName = 'Joe';
function greeting (userName){
if(userName !== ''){
console.log('Hello '+ userName + '!');
} else {
console.log('Hello!')
}
};
console.log(greeting(userName));
let userQuestion = 'Will I win the lottery this week?';
console.log( userName + ' wants to ask a question : ' + userQuestion);
let 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('The eight ball answered: ' + eightBall);
In this statement you call the greeting function. The function prints the greeting as specified inside of its code body. Since you donât have a return statement in the function, the function implicitly returns 'undefined' to the caller. In this case the caller is console.log(), so 'undefined' gets logged to the console after your greeting. To fix this behavior you can do one of two things. Either change your console.log() statements inside your function to return the greeting, or just invoke the function without the console.log() like so: greeting(userName).
let userName = 'Joe';
function greeting (userName){
if(userName !== ''){
console.log('Hello '+ userName + '!'); //change console.log to: return
} else {
console.log('Hello!') //same thing here
}
};
console.log(greeting(userName)); //Or change to: greeting(userName);
This is the only thing preventing your code from running. The else code block executes as a default if no prior if or else if conditions are truthy. You cannot test a condition in your else block. If randomNumber != to 0 through 6 the only other possible value is 7. Happy Coding!
Hi there! i managed to make the control flow work with the switch statements but when I use the if / else if the eight ball answer comes before the string:
Hello Martina!
The user asked
Cannot predict now
The eight ball answered:
Hereâs my code:
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 if (randomNumber === 7) {
console.log('Signs point to yes');
}
console.log(`The eight ball answered: ${eightBall}`);
In your code, you declaring eightBall as an empty string. When you determine what the answer will be with your ifâŚelse if statements, you are logging the answer to the console. This happens before your
statement. When this statement finally executes, eightBall is still empty. If you change console.log('answer'); to eightBall = 'answer'; you will get the outcome you were expecting.
Also, just a side note, your final else if only needs to be an else with no condition to test. If random number wasnât 0 - 6, 7 is the only value remaining that it could be, so else will suffice.
Happy coding!
const userQuestion = âWill I become a mermaid this summer?â;
console.log(The user asked: ${userQuestion});
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = ââ;
if (randomNumber == 0) {
console.log(âThe eight ball answered: It is certainâ);
} else if (randomNumber == 1) {
console.log(âThe eight ball answered: It is decidedly soâ);
} else if (randomNumber == 2) {
console.log(âThe eight ball answered: Reply hazy try againâ);
} else if (randomNumber == 3) {
console.log(âThe eight ball answered: Cannot predict nowâ);
} else if (randomNumber == 4) {
console.log(âThe eight ball answered: Do not count on itâ);
} else if (randomNumber == 5) {
console.log(âThe eight ball answered: My sources say noâ);
} else if (randomNumber === 6) {
console.log(âThe eight ball answered: Outlook not so goodâ);
} else if (randomNumber == 7) {
console.log(âThe eight ball answered: Signs point to yesâ);
} else {
console.log(âThe eight ball is brokenâ)
};
What code works perfectly without let eightBall = '';? Could you share the code in question? The declaration of the variable eightBall has a specific purpose the way it is used in the lesson. If you are using either a switch ... case block, or an if ... else if ... else block to assign a value to eightBall like so; eightBall = 'It is decidedly so.', without the aforementioned variable declaration, you end up declaring eightBall as a global variable at the time of assignment. In a small project like this, there is little to no consequence, but it is very bad practice to give any variable scope beyond itâs needs. It can lead to many problems in a large more complicated project. By using let eightBall = '' prior to assigning a value later, you are limiting the scope to the block of code in which it was declared including any nested code blocks.