toCurrency(100500, 'BYN', 'be-BY')
у меня возвращает строку "100 500,00 Br"
toCurrency(100500, 'BYN', 'ru-BY');
const re = new RegExp('(\\d{2}).(\\d{2}).(\\d{4})');
const tests = ['05.05.2020', '05/05/2020', '05/05/2020;22:40', '5', '5.1', '5.23'];
tests.forEach(str => console.log(`${str}: ${re.test(str)}`));
/*
05.05.2020: true
05/05/2020: true
05/05/2020;22:40: true
5: false
5.1: false
5.23: false
*/
ваши «тесты» проходит ) let bgIndex = 0;
appBackground.backgroundParrent.addEventListener('click', () => {
const url = backgroundArrayImg[(++bgIndex) % backgroundArrayImg.length];
document.querySelector('body').style.backgroundImage = `url(${url})`;
});
true
.price
.undefined
, если не найден.const data = [
{"nmId":22457195,"price":1947,"discount":30,"promoCode":0},{"nmId":22457420,"price":1400,"discount":27,"promoCode":0},
{"nmId":22458138,"price":1486,"discount":16,"promoCode":0},{"nmId":41678969,"price":1431,"discount":0,"promoCode":0}
];
const getPriceByNmId = nmId => data.find(item => item.nmId === nmId)?.price;
getPriceByNmId(22457195) // 1947
getPriceByNmId(100500) // undefined
const results = Object.values(arr.reduce((acc, c) => {
const prefix = c.id.split('.')[0];
acc[prefix] = [...acc[prefix] ?? [], c];
return acc;
}, {})
);
/* [
[{"id":"0"},{"id":"0.0"},{"id":"0.1"},{"id":"0.2"},{"id":"0.3"}],
[{"id":"1"},{"id":"1.0"},{"id":"1.1"}],
[{"id":"2"},{"id":"2.0"},{"id":"2.1"},{"id":"2.2"}],
] */
const getLeaders = numbers =>
numbers.reduceRight(
(acc, c) => { // acc – аккумулятор, c – очередной элемент массива
if (c > acc.sum) { // если больше суммы тех, что правее
acc.result.unshift(c); // положить в массив результатов
}
acc.sum += c; // накапливать сумму
return acc; // аккумулятор пойдёт в следующую итерацию
},
{ result: [], sum: 0 } // начальное значение acc
)
.result; // в итоге интересует только массив result
// использование
getLeaders([16, 17, 5, 4, 3, 1]) // [ 17, 3, 1 ]
const arrA = a[0].trim().split("\n");
// [ "123", "1234", "12345", "123456" ]
const arrB = b[0].trim().split("\n");
// [ "a", "b", "c", "d" ]
const arrAB = arrA.map((str, index) => `${arrB[index]}: ${str}`);
// [ "a: 123", "b: 1234", "c: 12345", "d: 123456" ]
arrAB.join("\n")
/*
"a: 123
b: 1234
c: 12345
d: 123456"
*/
const a = ['123', '1234', '12345', '123456'];
const b = ['a', 'b', 'c', 'd'];
const replace = (search, a, b) => {
const result = a.slice(); // копия
const index = a.indexOf(search);
if (index > -1) result.splice(index, 1, b[index]);
return result;
}
replace('1234', a, b)
// [ "123", "b", "12345", "123456" ]
async requestToSuip(suip, num_oper = '') {
const exampleData = {
result: false,
requisite: "",
amount: 0,
created: null,
suip: null
};
const url1 = "http://api-1/sber/cashout/suip/" + suip;
const url2 = "http://api-2/sber/cashout/txnid/" + num_oper;
const {data: data1} = await axios.get(url1);
const isEqual = Object.keys(exampleData)
.every(prop => exampleData[prop] === data1[prop]);
if (isEqual === false) {
return data1;
}
const {data: data2} = await axios.get(url2);
return {
...data1,
...data2, // поля из data2 перезапишут такие же из data1
};
}
function deep() {
function veryDeep() {
const inception = () => {
// во глубине сибирских руд:
globalThis.myGlobalVar = 'Habr'; // глобализм!
};
inception();
}
veryDeep();
}
deep();
console.log(myGlobalVar); // "Habr"
// random order in-place
const shuffle = array => {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i
[array[i], array[j]] = [array[j], array[i]];
}
};
// init
let coinIndex = -1;
const button = document.querySelector('.button');
button.addEventListener('click', () => {
if (coinIndex < 0) {
shuffle(coinFlip);
coinIndex = coinFlip.length - 1;
}
console.log(coinFlip[coinIndex]);
coinIndex--;
});
id
?const handleClick = id => () => {
document.querySelector('.container_wrp')?.remove();
const div = document.createElement('div');
div.style.width = '700px';
div.style.height = '500px';
div.id = id;
div.classList.add('container_wrp');
wrpBlock.appendChild(div);
};
jccBtnJap.addEventListener('click', handleClick('jap_container'));
jccBars.addEventListener('click', handleClick('stick_container'));
тут handleClick()
возвращает функцию, которая даёт создаваемому элементу переданный id. Thefor...in
statement iterates over all enumerable properties of an object
that are keyed by strings
(ignoring ones keyed by Symbols),
including inherited enumerable properties.
const m = new Map();
m.set({}, 'obj');
m.set('a', 'A');
// это другое
m.b = 'B';
for (let prop in m) {
console.log('property', prop);
}
// property b