Logical operators - some explnation needed


#1

my code is running , but is that all ? i don't understand about 'case=0' .

var user = prompt(" what is your favorite sports ?").toLowerCase();

switch(user)
{
case 0:
if(user == 'football' || user === 'soccer')
{
console.log('very winderfull game');
}
else
{
console.log('you dont like this game');
}
break;


case 'weightleffting':
if(user == 'weightleffting' && user === 'box')
{
console.log('very winderfull game');
}
else
{
console.log('you dont like this game');
}
break;



default:
console.log("Now u can like any game");
break;
}

#2

"After your prompt, add a switch statement that will test for several different cases (that is, different possible user inputs). Create as many as you like! (Do at least three.)"

Well, first off, a case is if the user types in a certain answer then they get "switched" to the if and else statements under that case. Basically, a chain reaction in your code to get questions and responses in this situation. The 0 in the case is the answer that the user has to type into the prompt to further prompt the switch. It is like the key to unlocking the switch. This is why a default case is so important because if no answer to the prompt matches any of your cases the code needs something to fall back on so it can still completely run. Another example of this is in my code:

var user = prompt("What is your name").toUpperCase();
switch(user) {
    case 'BRAD':
        console.log("That is my name too!");
        break;

If the user in the prompt answers Brad then the code will switch to the case that has Brad in it and the user will see this response on the console "That is my name too!" However, if they type in Steve, they will not get that because they did not answer with that word to switch to that particular case.

Hopefully this helps and has been at least a decent explanation. It does not go to deep in to technicallities so if you would like that I can find some articles or such that explain it a little bit more deeper or get another moderator or help desker to explain it for you. :smiley:


#3

thanks , u have done a lot for me , but kindly make my code with switch explanation as u say and give the airticles plz :slight_smile:


#4

Ok, so, in getting this all straight, your case 0 is not giving anything when you type in 0 into the prompt or am I missing your main issue? Is 'case=0' an error message you are getting? Sorry, just trying to get this all straight so I can help you more. Well I just put your code into my lesson and it works fine so what do you want fixed? Also, I can give articles in a little bit got to find them xD


#5

ok , case=o is error msg , i got that , but how? , is that link to true or false ? [1=true; 0=false];

u say to write switch statement in each case , if u dont mind , kindly edit my code and give me the article to know deeply .


#6

Well first off, I don't get that error message when I put your original code into my lesson. It passes. Also, I did not say write switch statement for each case. You just need one switch statement with multiple cases. Also, I am searching for articles to give you so be patient just a little :smiley:


#7

Ok here are some articles:

http://www.w3schools.com/jsref/jsref_switch.asp


http://www.tutorialspoint.com/javascript/javascript_switch_case.htm

Ok the first two do a good job with indepth explanations. The third one provides really good examples and pictures to go along with the explanation. Hopefully these can help you in your effort to learn!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.