0

I have this code in JavaScript from an example I'm learning from. I think the code is correct but I don't know how I would make it display itself. Can anyone help?

var num =2;
var bool = false;

if(num ==1 && bool==1) alert("TEST1 bool: "+ bool);
else

if(num ==2 && bool==1) alert("TEST2 bool: ")+ bool);
else

if(num ==2 && bool==0) alert("TEST3 bool: ")+ bool);
else

if(num ==3 && bool==0) alert("TEST4 bool: ")+ bool);

Thankyou in advance.

3
  • It's not clear what you want. Can you try explaining your problem, and what you've tried so far. Commented Apr 14, 2015 at 8:27
  • @CodeLღver bool is not a reserved word. Commented Apr 14, 2015 at 8:28
  • 2
    follow @sifriday advice, open web developer tools, switch to console, paste you code and run. Or put this code in file and include it with <script> to some html page and load it in browser Commented Apr 14, 2015 at 8:29

3 Answers 3

1

You can do this in the browser console, or with a tool like jsfiddle, or by saving it to an HTML file and opening that file in your browser.

You do also have a typo in your code - you need to remove some brackets. And if you are going to define bool = false then be aware that when you compare it to 1 and 0 this only works because JS is loosely typed. 1 is coerced to be equivalent to true and 0 is coerced to be equivalent to false.

var num =2;
var bool = false;

if(num ==1 && bool==1) alert("TEST1 bool: "+ bool);
else

if(num ==2 && bool==1) alert("TEST2 bool: "+ bool);
else

if(num ==2 && bool==0) alert("TEST3 bool: "+ bool);
else

if(num ==3 && bool==0) alert("TEST4 bool: "+ bool);

JSFiddle Example

http://jsfiddle.net/mx3eyhxf/

JSFiddle is nice for this kind of thing because you can load in libraries like jQuery really easily.

Browser Console

As @Evegeniy says in his comment above, to do this simply open your web developer tools, switch to console, paste your code and run.

File

Create an HTML file like:

<html> 
    <head>
        <script type="text/javascript">
            var num =2;
            var bool = false;

            if(num ==1 && bool==1) alert("TEST1 bool: "+ bool);
            else

            if(num ==2 && bool==1) alert("TEST2 bool: "+ bool);
            else

            if(num ==2 && bool==0) alert("TEST3 bool: "+ bool);
            else

            if(num ==3 && bool==0) alert("TEST4 bool: "+ bool);
        </script>
     </head>
     <body></body>
</html>

and open that in your browser.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is attracting some controversy! It is currently -2 and +2. Any insight from the downvoters as to why, then I can improve it?
0

There is an extra parenthesis... Moreover you can remove else statements in this case.

var num =2; var bool = false;
if(num ==1 && bool==1) alert("TEST1 bool: "+ bool);
if(num ==2 && bool==1) alert("TEST2 bool: "+ bool);
if(num ==2 && bool==0) alert("TEST3 bool: "+ bool);
if(num ==3 && bool==0) alert("TEST4 bool: "+ bool);

Comments

0

var bool is of type boolean use bool for bool == 1 and !bool for bool==0 also there is one extra parenthesis in alert

 var num =2; var bool = false;

  if(num ==1 && bool) 
        alert("TEST1 bool: "+ bool); 
  else if(num ==2 && bool) 
       alert("TEST2 bool: "+ bool); 
 else if(num ==2 && !bool) 
      alert("TEST3 bool: "+ bool); 
 else if(num ==3 && !bool) 
     alert("TEST4 bool: "+ bool);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.