1

It's possible to write in one line something like this?

let value1 = 10;
let value2 = 20;

let result = ( value1 - value2 ) / ( value1 - 1);
if (result<0)
 result = 0;

Something like this (but without having to rewrite the formula 2 times):

let result = ( ( ( value1 - value2 ) / ( value1 - 1) ) < 0 ) ? 0 : ( ( value1 - value2 ) / ( value1 - 1) );
kurtko
  • 1,978
  • 4
  • 30
  • 47
  • `Math.max` exists ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max – CBroe Jan 15 '18 at 12:23

2 Answers2

3

You could use Math.max with zero as another parameter.

let value1 = 10;
let value2 = 20;
let result = Math.max((value1 - value2) / (value1 - 1), 0);

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

the logic with the ans with Math.max is good.

you also can try with following:

var value1 = 10;
var value2 = 20;
var v;
var result = (( v = (( value1 - value2 )/(value1 - 1))) < 0 ) ? 0 : v;

console.log(v);
jasmin_makasana
  • 472
  • 3
  • 11