JavaScript test framework running on Node.js and in the browser
const sum = (a, b) => a + b;
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
const sum = (a, b) => a + b;
function testSum(description, a, b, expected) {
const result = sum(a, b);
if (result === expected) {
console.log(`✓ ${description}`);
} else {
console.error(`✗ ${description}: expected ${expected} but got ${result}`);
}
}
testSum('adds 1 + 2 to equal 3', 1, 2, 3);
var option = {
scale: 1.2
};
var option = {
transform: ["scale(1.2)", "scale(1)"]
};
function SliderRange() {
var moveHandler = function () {
var inputMinVal = $(this).siblings('.from').find('.b-filter-price__input');
var inputMaxVal = $(this).siblings('.to').find('.b-filter-price__input');
if ($(inputMinVal)[0].value === '') {
$(inputMinVal)[0].value = $(this).find('.slider-range__input-min').val();
}
if ($(inputMaxVal)[0].value === '') {
$(inputMaxVal)[0].value = $(this).find('.slider-range__input-max').val();
}
};
this.init_function = function() {
$('.g_slider_range').each(moveHandler);
}
this.init_function();
$('.g_slider_range').on('mousedown', function () {
$(this).on('mousemove', moveHandler);
})
}
function chooseBestSum(t, k, ls) {
/**
* make next combination of N on bits in a 32-bit integer
*/
function nextPerm(x) {
// via https://stackoverflow.com/questions/506807/creating-multiple-numbers-with-certain-number-of-bits-set
if (x === 0) return 0;
const smallest = (x & -x);
const ripple = x + smallest;
const new_smallest = (ripple & -ripple);
const ones = ((new_smallest/smallest) >> 1) - 1;
return ripple | ones;
}
let bestSum = null, bestN;
const len = ls.length;
if (len > 31) throw "Too many (over 31) options for this algorithm";
const maxmask = (1 << len) - 1;
if (len < k) return null; // not enough distances
ls.sort((a, b) => a - b); // todo: skip checking rest of combinations once solid selection of elements exceeds t
let mask = (1 << k) - 1; // initial mask value with k less significant bits on
let sum, pos, n;
while(true) {
for(sum = 0, n = 0, pos = 0; pos < 32; pos++) {
if (mask & (1 << pos)) {
sum += ls[pos];
if (++n === k) break;
}
}
if (sum > bestSum && sum <= t) {
bestSum = sum;
bestN = mask;
}
mask = nextPerm(mask);
if (mask > maxmask) break;
if (mask < 0) break;
}
return bestSum;
}