Javascript calculation without a negative number

Tags: javascript
Question!

How can I make sure that I don't get a negative number out of this code:

function reduction() {
  ctrlreduct.setValue(Number(ctrl1.getValue()) + Number(ctrl2.getValue()) - Number(ctrl3.getValue()));
};

I would like any negative number to default to zero.

By : hvilberg


Answers

ok, I now have something that works:

function reduction() {
var test = (Number(ctrl1.getValue()) + Number(ctrl2.getValue()) - Number(ctrl3.getValue()));
ctrlreduct.setValue(Math.max(0,test));
};

Thx for the help :)

By : hvilberg


Just make an if decision...

if (( ctrlreduct.setValue(Number(ctrl1.getValue()) + Number(ctrl2.getValue()) - Number(ctrl3.getValue())) ) < 0 )
    {
        ctrlreduct.setValue(0) ;
    }


in the constructor, you just need to do this.state = { video: {} } not this.setState

constructor(){
    super();
    this.state = {
        video:{}
    };
}

this.setState can be used anywhere else but the constructor. non es6 way of doing this was doing:

getInitialState: function() {
  return { 
    video: {}
  }
}

which is equivalent to just doing this.state = {} in the constructor

By : finalfreq


This video can help you solving your question :)
By: admin