0

I don't understand why if-else construction doesn't work.

const Diagnostics = require('Diagnostics');
const Reactive = require('Reactive');


var num = 5;
var firstCondition= false;
var secondCondition = false;

function func(){
    firstCondition = Reactive.ge(num, 10);
    secondCondition = Reactive.lt(num, 10);
    if(firstCondition){             //false
      num = 0;
    } else if(secondCondition){     //true
      num = 1;
    }
}
func();

Diagnostics.watch("num - ", num);
Diagnostics.watch("firstCondition ", firstCondition);
Diagnostics.watch("secondCondition ", secondCondition);

num shows 0:( What am I doing wrong? Is it specific of reactive programming?

biomusor
  • 17
  • 7

3 Answers3

2

There are two issues in your example:

  • signals can be confusing and return true just because the variable is there, so you need to check: if (firstCondition.pinLastValue()) to get the value of the boolean signal.
  • secondly, I guess your function is not updated, you need something like this:

    const Time = require('Time'); const interval = Time.setInterval(func(), 500);
    
1

You need to use different mindset here:

const conditionedSignal = inputSignal.ge(10).ifThenElse(0, 1);

This is a scalarSignal class, not a JS Number!

lisichka ggg
  • 563
  • 3
  • 15
0

Spark AR Studio uses reactive programming, a declarative programming model that uses asynchronous data streams. This guide will cover the benefits and use of reactive programming within Spark AR Studio.

https://sparkar.facebook.com/ar-studio/learn/documentation/scripting/reactive/

Nithin NS
  • 51
  • 6