WITH
`cte1` AS (
SELECT `to_user_id` AS `respondent`, `message`, `datetime`,
'outgoing' AS `direction`
FROM `messages`
WHERE `from_user_id` = :userId
UNION ALL
SELECT `from_user_id`, `message`, `datetime`,
'incoming' AS `direction`
FROM `messages`
WHERE `to_user_id` = :userId
),
`cte2` AS (
SELECT `respondent`, `message`, `datetime`, `direction`,
ROW_NUMBER() OVER `w` AS `row_num`
FROM `cte1`
WINDOW `w` AS (
PARTITION BY `respondent`
ORDER BY `datetime` DESC
)
)
SELECT `respondent`, `message`, `datetime`, `direction`
FROM `cte2`
WHERE `row_num` = 1
JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).Кратко - с опциями g и y объект RegExp сохраняет позицию, с которой продолжает поиск при следующем вызове. У вас одинаковые строки и второй вызов начинает с позиции, на которой закончился первый вызов, то есть уже после '.mp3'.
countCookies(3, 6, [0, 0, 0]); // 1
countCookies(3, 2, [0, 2, 2]); // 0
- const allCookies = cookies.reduce((sum, acc) => sum + acc, 0);
- let maxCookies = Number(Math.max.apply(null, cookies));
+ const maxCookies = Math.max(...cookies);
- let sum = cookies.reduce((acc, c) => acc + Math.floor((c + K - 1) / K), 0);
+ const sum = cookies.reduce((acc, c) => acc + Math.ceil(c / K), 0);