const result = elements.filter(n => !used.some(m => m.id === n.id));
const result = elements.filter(((ids, n) => !ids.includes(n.id)).bind(null, used.map(n => n.id)));
const result = elements.filter(function(n) {
return !this.has(n.id);
}, new Set(used.map(n => n.id)));
const result = Object.values(used.reduce(
(acc, n) => (delete acc[n.id], acc),
elements.reduce((acc, n) => (acc[n.id] = n, acc), {})
));
const result = [...used.reduce(
(acc, n) => (acc.delete(n.id), acc),
new Map(elements.map(n => [ n.id, n ]))
).values()];
$.getJSON('https://api.cdnjs.com/libraries/jquery', function(data) {
$('body').append(`<pre>${JSON.stringify(data, null, 2)}</pre>`);
});
const toHTML = val =>
val instanceof Object
? `<ul>${Object.values(val).map(n => `
<li>${toHTML(n)}</li>`).join('')}
</ul>`
: val;
$.getJSON('https://api.cdnjs.com/libraries/jquery', function(data) {
$('body').append(toHTML(data));
});
const type = document.querySelector('#type');
const price = document.querySelector('#price');
const minPrices = {
bungalo: 0,
flat: 1000,
house: 5000,
palace: 10000,
};
type.addEventListener('change', function() {
const minPrice = minPrices[this.value];
price.min = minPrice;
price.value = Math.max(minPrice, price.value);
});
type.dispatchEvent(new Event('change'));
$('body').on('click', '.help'
$('body').on('mousedown', '.help'
function maxZeroSequence(arr) {
let iMin = 0;
let iMax = 0;
for (let i = 0; i < arr.length; i++) {
let sum = 0;
for (let j = i; j < arr.length; j++) {
if ((sum += arr[j]) === 0 && (iMax - iMin) <= (j - i + 1)) {
iMin = i;
iMax = j + 1;
}
}
}
return arr.slice(iMin, iMax);
}
function maxZeroSequence(arr) {
const sumIndices = Object.values(arr.reduce((acc, n, i) => {
const sum = acc[0] += n;
(acc[1][sum] = acc[1][sum] || [ i + 1 ])[1] = i + 1;
return acc;
}, [ 0, { 0: [ 0 ] } ])[1]);
return arr.slice(...sumIndices.reduce((max, n) => {
const diff1 = n[1] - n[0];
const diff2 = max[1] - max[0];
return diff1 > diff2 || (diff1 === diff2 && n[0] > max[0]) ? n : max;
}, [ 0, 0 ]));
}
const cities = [ 'Москва', 'Санкт-Петербург', 'Севастополь' ];
if (cities.includes(geolocation.city)) {
...
<div class="slider-wrapper">
<button data-step="-1">-</button>
<div class="slider"></div>
<button data-step="+1">+</button>
</div>
$('.slider').slider({ /* настройки слайдера */ });
$('.slider-wrapper [data-step]').button().click(function() {
const $slider = $(this).closest('.slider-wrapper').find('.ui-slider');
const step = +this.dataset.step;
const { min, max, value } = $slider.slider('option');
$slider.slider('option', 'value', Math.max(min, Math.min(max, +value + step)));
});
const add = (str, num, saveLength) =>
`${+str + num}`.padStart(str.length, 0).slice(-str.length * !!saveLength);
add('0001', 1) // '0002'
add('000099', 901) // '001000'
add('05', 99) // '104'
add('05', 99, true) // '04'
19
, или true
, или '!!!!!'
, или... у вас же в операторе switch отсутствует ветка default - и вы ещё спрашиваете, откуда берётся undefined.case 1: return outtype = 'a'; break;
const getType = (type) => ({
1: 'a',
2: 's',
3: 'f',
})[parseInt(type, 10)] || /* а здесь должно быть дефолтное значение */
function validateInput($input, regex) {
const result = regex.test($input.val());
$input
.closest('.form-group')
.removeClass('has-success has-error')
.addClass(result ? 'has-success' : 'has-error');
return result;
}
function validate() {
return [
validateInput($('#name'), /^[a-z]{2,16}$/i),
validateInput($('#message'), /^[a-z]{15,100}$/i),
].every(Boolean);
}
$('form').on({
input: validate,
submit(e) {
if (!validate()) {
e.preventDefault();
}
},
});
const div = document.createElement('div');
div.innerHTML = str;
const arr = Array.prototype.map.call(div.children, n => n.outerHTML);
const arr = Array.from(
new DOMParser().parseFromString(str, 'text/html').body.children,
n => n.outerHTML
);
const arr = [];
for (const n of document.createRange().createContextualFragment(str).children) {
arr.push(n.outerHTML);
}
let number = 0b1101;
const count = [ 0, 0 ];
do {
count[number & 1]++;
} while (number >>= 1);
count.forEach((n, i) => console.log(`${i}: ${n}`));
const count = [...number.toString(2)].reduce((acc, n) => (++acc[n], acc), [ 0, 0 ]);
var lastCharacter = ';';
$('input').on('input', function() {
var val = this.value;
if (val && val[val.length - 1] !== lastCharacter) {
$(this).val(val + lastCharacter);
}
});