Javascript evaluating undefined expressions in OR statement -
so have statement looks this
subsetsumarray[i][j]=(subsetsumarray[i-1][j] || subsetsumarray[i-1][j-arr[i]]);
the problem when variable j less arr[i], subsetsumarray[i-1][j-arr[i]] return undefined. if subsetsumarray undefined treated false. whats shortest way this. i'd want work
(some undefined variable || true) ===> return true
thanks in advance help!
the shortest way double negative (!!
) trick:
subsetsumarray[i][j]=!!(subsetsumarray[i-1][j] || subsetsumarray[i-1][j-arr[i]]);
see below code , run it:
// empty array var = []; // var[1] undefined alert( a[1] ); // undefined alert(!! a[1] ); // false alert(a[1] || true); // true alert( a[1] || a[2] ); // undefined alert( !! (a[1] || a[2]) ); // false
Comments
Post a Comment