重複範囲があるかの if 文
- カテゴリ:
- JavaScript
- コメント数:
- Comments: 0
◆ 2 つの範囲に重複場所があるかどうか
必要になるたびに紙に書いて考えてるのでメモ
null は対応してない
null 対応するなら
from と to が閉じた範囲じゃないときは対応してない
function duprange(r1, r2){
return r1.from <= r2.to && r2.from <= r1.to
}
duprange(
{from: 1, to: 10},
{from: 8, to: 12},
)
// true
return r1.from <= r2.to && r2.from <= r1.to
}
duprange(
{from: 1, to: 10},
{from: 8, to: 12},
)
// true
null は対応してない
duprange(
{from:null, to:10},
{from:-20, to:-10}
)
// false
// 10以下と -20~-10 なので true のはず
{from:null, to:10},
{from:-20, to:-10}
)
// false
// 10以下と -20~-10 なので true のはず
null 対応するなら
const duprange2 = (r1, r2) =>
(r1.from != null && r2.to != null ? r1.from <= r2.to : true)
&&
(r2.from != null && r1.to != null ? r2.from <= r1.to : true)
(r1.from != null && r2.to != null ? r1.from <= r2.to : true)
&&
(r2.from != null && r1.to != null ? r2.from <= r1.to : true)
from と to が閉じた範囲じゃないときは対応してない
duprange2(
{from:3, to:-3},
{from:10, to:20}
)
// false
// 3以上または-3以下 と 10~20 だから true のはず
{from:3, to:-3},
{from:10, to:20}
)
// false
// 3以上または-3以下 と 10~20 だから true のはず