Search In
• More options...
Find results that contain...
Find results in...

# if statement in javascript?

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!

##### 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!");

}```

##### 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.

##### 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?

##### 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!

##### Share on other sites

`Math.floor(Math.random() * 10) + 1; `

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.

##### Share on other sites

1 minute ago, bleedblue said:

```
Math.floor(Math.random() * 10) + 1; ```

Thanks so much!

##### Share on other sites

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