// Array.
const array = [{lowlimit: 0, highlimit: 100}, {lowlimit: 201, highlimit: 300}]
// Should Push.
const shouldpush = (array, object) => {
const length = array.length
for (let i = 0; i <= (length-1); i ++) {
// Comps.
const pre = array[i] || false
const post = array[i+1] || false
// Cases:
// Length(1) AND Fit Above (Extreme Edge Case).
if ((length == 1) && (pre.highlimit < object.lowlimit)) return [true, [...array, object]]
// First Fit (Below).
const atfirst = (i == 0) // At First Index.
const firstfit = (atfirst && (pre.lowlimit > object.highlimit)) // First Fit.
if (firstfit) return [true, [object, ...array]] // True.
// Normal Fit.
const lowfit = (object.lowlimit > pre.highlimit) // Standard Low.
const highfit = (object.highlimit < post.lowlimit) // Standard High.
if (lowfit && highfit) return [true, [...array.slice(0, i+1), object, ...array.slice(i+1)]]
// Last Fit (Above).
const atlast = (i == (length - 2)) // At Last Index.
const lastfit = (atlast && (post.highlimit < object.lowlimit)) // Last Fit.
if (lastfit) return [true, [...array, object]] // True.
// No Fit.
if (!lowfit && !highfit) return [false, [...array]] // !Fit (Early Exit).
}
return [false, [...array]] // !Fit.
}
// Test Objects.
const w = {lowlimit: 301, highlimit: 400} // W.
console.log(shouldpush(array, w)) // True.
const x = {lowlimit: 101, highlimit: 150} // X.
console.log(shouldpush(array, x)) // True.
const y = {lowlimit: 50, highlimit: 80} // Y.
console.log(shouldpush(array, y)) // False.
const z = {lowlimit: 50, highlimit: 250} // Z.
console.log(shouldpush(array, z)) // False.
// Example Retrieval.
const [push, newarray] = shouldpush(array, x)
console.log(push, newarray) // Push. New Array.