const range = arr => arr
.sort((a, b) => a - b)
.reduce((agg, v) => {
const currRange = agg[agg.length - 1]
if (!currRange || v > currRange.last + 1) {
agg.push({first: v, last: v})
} else {
currRange.last = v
}
return agg
}, [])
.map(v => v.first + (v.first !== v.last ? '-' + v.last : '')).join()
Math.max(arrMultiplication.map(v => isNaN(v) ? -Infinity : v))
const deepMerge = (a, b, howDeep = 1000) => {
a = {...a}
Object.entries(b).forEach(([key, value]) => {
if (typeof value !== 'object' || howDeep < 1) {
a[key] = value
} else {
a[key] = deepMerge(a[key], value, howDeep - 1)
}
})
return a;
}
const c = deepMerge(a, b)
console.log(c)
const conditions= [
'and',
a === 3,
b === 7,
c !== 4,
['or', d < 15, c !== 5]
]
const countValue = (arr) => {
if(!Array.isArray(arr)) return arr
const cond = arr[0] === 'or'
? Array.prototype.some
: Array.prototype.every
return cond.bind(arr.slice(1), countValue)()
}
const conditions = [
'or',
['and', a === 7, b === 4],
['and', a < 8, b < 5]
]
console.log('result:' + countValue(conditions))
for (let i = 4; i <= num; i = i + 3) {
for (let i = 14; i <= num; i = i + 7) {
for (let i = 2; i <= num; i++) {
if (i % 2 !== 0) {
numArr.push(i);
}
}
const simpleNumbersSum = max => {
const arr = new Array(max + 1)
let sum = 1 + 2; //Или просто 2, если единицу не учитываем
for (let i = 3; i <= max; i+=2) {
if (!arr[i]) {
sum+=i;
for (let j = i * i; j <= max; j+=i*2) {
arr[j] = true;
}
}
}
return sum
}
console.log(simpleNumbersSum(977))
new Set([...arr1, ...arr2]).size < arr1.length + arr2.length
arr1.find(v => arr2.includes(v))
function arrayHaveMatches(arr1, arr2) {
const arrays = [arr1, arr2]
const indexes = [0, 0]
let currArray = arr2[0] < arr1[0]
let val2 = arrays[!currArray][0]
while (indexes[currArray] < arrays[currArray].length) {
const val1 = arrays[currArray][indexes[currArray]]
if (val1 === val2) {
return true
} else if (val1 > val2) {
currArray = !currArray
val2 = val1
}
indexes[currArray] ++
}
return false
}