Search code examples
javascriptspark-ar-studio

If/Else condition is changing the value of variable (SparkAR)


I'm facing this weird problem within SparkAR.

I was trying to pull data from the patch editor and passing it to a if/else but it wasn't working. I removed everything to see where the problem is and it seems like this code:

function compareAlpha(){
    var alpha = 0;
    if (alpha = 1){
      Diagnostics.log('alpha is 1')
    } else {
      Diagnostics.log('alpha is not 1')
    }
  };
  compareAlpha();

Shows up as "alpha is 1" which to me sounds crazy. I'm declaring alpha as 0 then the if condition is changing it to 1??

Sorry if it's a very stupid question, I'm just starting coding and the documentation on spark website isn't that easy to understand.


Solution

  • function compareAlpha(){
        var alpha = 0;
        if (alpha === 1){
          Diagnostics.log('alpha is 1')
        } else {
          Diagnostics.log('alpha is not 1')
        }
      };
      compareAlpha();

    pleasse use == or === to check condition otherwise it will not work. there is mutiple use of = sign if you use 1 time = its mean you are intilize a value in variale and if you use 2 o 3 = then its mean you are comparing right hand side to left hand side