deepEqual({a: 1}, {b: 1}); // true
deepEqual(null, {b: 1}); // 1
deepEqual({a: 1, b: 2}, {b: 2, a: 1}); // falseconst deepEqual = (a, b) => {
if (a === b) {
return true;
}
if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') {
return false;
}
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
for (let i = 0; i < aKeys.length; i += 1) {
const key = aKeys[i];
if (!bKeys.includes(key) || !deepEqual(a[key], b[key])) {
return false;
}
}
return true;
};listToArray(arrayToList([1, 2, 3])); // Array(3) [ 3, 2, 1 ]const list = {
value: 2,
rest: {
value: 3,
rest: null,
},
};
prepend(1, list);
/*
Object {
value: 1,
rest: Object {
value: 3,
rest: Object {
value: 2,
rest: null
}
}
}
*/const arrayToList = (arr, idx = 0) => ({
value: arr[idx],
rest: idx === arr.length - 1
? null
: arrayToList(arr, idx + 1)
});
const listToArray = (list) => (
list.rest === null
? [list.value]
: [list.value, ...listToArray(list.rest)]
);
const prepend = (value, rest) => ({ value, rest });
const nth = (list, idx) => (
idx === 0
? list.value
: (
list.rest === null
? undefined
: nth(list.rest, idx - 1)
)
);const arrayToList = (arr) => {
const list = null;
for (let i = arr.length - 1; idx >= 0; idx -= 1) {
list = { value: arr[idx], rest: list };
}
return list;
};
const listToArray = (list) => {
const arr = [];
while (list !== null) {
arr.push(list.value);
list = list.rest;
}
return arr;
};
const prepend = (value, rest) => ({ value, rest });
const nth = (list, idx) => {
while (list !== null) {
if (idx === 0) {
return list.value;
}
idx -= 1;
list = list.rest;
}
};const test = (x) => { console.log(x); return false; };
if (test(1) && test(2)) {
console.log('???');
} sort(array &$array, int $flags = SORT_REGULAR): bool
const NAMESS = [];
while (NAMESS.length < 8) {
const rand = `${NAMES[random(0, NAMES.length - 1)]} ${SURNAMES[random(0, SURNAMES.length - 1)]}`;
if (!NAMESS.includes(rand)) {
NAMESS.push(rand);
}
} [0, 1, 2, 3, 4].map(
(shift) => {
const date = new Date();
date.setDate(date.getDate() + shift);
return date.toLocaleDateString('en-CA');
}
);
// Array(5) [ "2022-03-28", "2022-03-29", "2022-03-30", "2022-03-31", "2022-04-01" ]const date = new Date();
const dates = [];
for (i = 0; i < 5; i += 1) {
dates.push(date.toLocaleDateString('en-CA'));
date.setDate(date.getDate() + 1);
}
console.log(dates);
// Array(5) [ "2022-03-28", "2022-03-29", "2022-03-30", "2022-03-31", "2022-04-01" ]