Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

if statement in javascript?

Cookiecrumbles222
 Share

Go to solution Solved by ideaeclipse,

the single equals sign is for declaring variables while the double equal sign is for comparing two thing

if((a1 + a2) == num){
       console.log("Correct!");
}else{
       console.log("Incorrect!");

}

 

hey guys really simple javascript question I'm confused on. how do I do something like this in javascript, what am I doing wrong?

 

            var a1 = 1;
            var a2 = 2;
            var num = prompt(a1 + " " + "+" + " " + a2 + " = " + "?");
            
            if(a1 + a2 = num){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }

 

Edit: also how can I make var a1 and a2 a random number between 1 and 10 Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

the single equals sign is for declaring variables while the double equal sign is for comparing two thing

if((a1 + a2) == num){
       console.log("Correct!");
}else{
       console.log("Incorrect!");

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

//Please format code like this next time


if(a1 + a2 = num) //you are assigning the value of num here

//you need to do it like this
if(a1 + a2 == num)

 

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, bleedblue said:

//Please format code like this next time


if(a1 + a2 = num) //you are assigning the value of num here

//you need to do it like this
if(a1 + a2 == num)

 

yup that fixed it thanks! and sorry for not doing the format thing idk how to, also how do i random a number between 1-10?

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, ideaeclipse said:

the single equals sign is for declaring variables while the double equal sign is for comparing two thing


if((a1 + a2) == num){
       console.log("Correct!");
}else{
       console.log("Incorrect!");

}

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Cookiecrumbles222 said:

hey guys really simple javascript question I'm confused on. how do I do something like this in javascript, what am I doing wrong?

 

            var a1 = 1;
            var a2 = 2;
            var num = prompt(a1 + " " + "+" + " " + a2 + " = " + "?");
            
            if(a1 + a2 = num){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }

 

Edit: also how can I make var a1 and a2 a random number between 1 and 10 Thanks!

also instead of doing var num = prompt(a1 + " " + "+" + " " + a2 + " = " + "?");
do : var num = prompt(a1 + " + " + a2 + " = " + "?");
 the result will be the same but the code  looks cleaner

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×