I have the following result from an API
data: {
"total_slots": int,
"occupied_count": int,
"occupied_slots" : [int] e.g.[452,453,459] (here each int in the array signfies an occupied slot),
"slots_down" : [int] e.g.[460,462] (here each int in the array signfies a down slot)
}
I want the following conditions
VAR OCCUPIED, length of the list which are occupied minus length of common slots in occupied and slots_down
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down
VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
The slots are 31 that are fixed.
var ALL_SLOTS = [452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523];
How can I meet the OR/AND conditions like VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
This is my CURRENT CODE
s = JSON.parse(s);
console.log(s);
SLOTS_DOWN = s.slots_down;
var total_slots = s.total_slots;
SLOTS_DOWN = s.slots_down.length;
console.log('down slots are ' + SLOTS_DOWN);
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN);
var occupied = s.occupied_count;
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN);
According to these:
VAR OCCUPIED, length of the list which are occupied minus length of common
slots in occupied and slots_down
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down
VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
you could try this:
var OCCUPIED = data.occupied_count -
data.occupied_slots.filter(function(elem){
return data.slots_down.indexOf(elem)>-1;
}).length;
var TOTAL_SLOTS = data.total_slots.length - data.slots_down.length;
var AVAILABLE = ((data.total_slots.length - data.slots_down.length)) -
data.occupied_slots.filter(function(elem){
return data.slots_down.indexOf(elem)==-1;
}).length
This should work:
data = {
"total_slots": 124,
"occupied_count": 3,
"occupied_slots" : [245, 326, 256],
"slots_down" : [245, 136]
}
var down_but_not_occ = 0;
data.slots_down.map(function(v, i){
if(data.occupied_slots.indexOf(v) === -1){
down_but_not_occ ++;
};
})
var available_slots = parseInt(data.total_slots) - data.occupied_slots.length - down_but_not_occ;
console.log(available_slots);
Where you were wrong,
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN); // slots down subtracted
var occupied = s.occupied_count;
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN); // slots down AGAIN subtracted
Related
why it is showing me 36 even though the minimum number is 27
var combination = [27, 36]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min)
i tried this multiple ways like
var combination = [27, 30, 40, 44, 3, 239, 329, 2, 5, 20923, 96]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min) //output-- 5 //it should be 2
in this third example i add (-) to 2
var combination = [27, 30, 40, 44, 3, 239, 329, -2, 5, 20923, 96]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min) // output-- still 5 // it should be -2
again when am adding (-) to other numbers like in -96 or -5 the output was okay (-96) but when im adding (-) to 2 it is not showing me -2 in the output instead it showing me 5
not only in javascript i tried this with lua, php but output was same as js
can anyone explain me why this happen and how solve this
You're not comparing values to determine the minimum, but instead just replacing the min variable with the last number in the array that is smaller than 50. This can be fixed as follows:
let min = undefined;
for (let x in combination) {
if (combination[x] < 50) {
min = min == undefined ? combination[x] : Math.min(min, combination[x])
}
}
Using filter and reduce, this can be made a lot shorter:
combination.filter(x => x < 50).reduce((x, y) => Math.min(x, y))
I have a number (let's say 525). I would like to take this number and split it into an array of chunks with a max of 100 each value. If I took 525 and split it into an array, it would look like:
[
100,
100,
100,
100,
100,
25
]
Here's what I've tried so far:
var number = 525;
var array = [];
while (number > 0) {
number = number - 100;
array.push(Math.min(number, 100));
}
That doesn't get me far. It just returns [ 100, 100, 100, 100, 25, -75 ]. I know that using while isn't the best way to go, but that is what I could think of off the top of my head. Does anyone have any other way that improve my code and be more efficient?
You can figure the number of times that number is divisible by 100 then programmatically create an array of that length:
var number = 525;
var array = new Array(Math.floor(number / 100)).fill(100).concat(number % 100))
// [ 100, 100, 100, 100, 100, 25 ]
You can extend this to chunk by any number:
function chunkBy(number, n) {
var chunks = Array(Math.floor(number / n)).fill(n);
var remainder = number % n;
if (remainder > 0) {
chunks.append(remainder);
}
return chunks;
}
Alternatively, simply push the element before performing your subtraction:
var number = 525;
var array = [];
while (number > 0) {
array.push(Math.min(number, 100));
number = number - 100;
}
// [ 100, 100, 100, 100, 100, 25 ]
Using ES6 with arrow functions:
const chunkBy = (n) => number => {
var chunks = new Array(Math.floor(number / n)).fill(n);
var remainder = number % n;
console.log('CHUNKS = ', chunks);
if (remainder > 0) {
chunks.push(remainder);
}
return chunks;
};
const chunkBy50 = chunkBy(50);
const chunkBy100 = chunkBy(100);
const chunkBy77 = chunkBy(77);
console.log(chunkBy50(500));
// [ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 ]
console.log(chunkBy100(500));
// [ 100, 100, 100, 100, 100 ]
console.log(chunkBy77(500));
// [ 77, 77, 77, 77, 77, 77, 38 ]
Alternatively, you could use integer division to get the number of occurrences for the "max" divisor and then use the "mod" operation to get the final chunk or remainder.
const number = 525;
const max = 100;
const maxCount = Math.floor(number / max); // 5
const remainder = 525 % max; // 25
You should move the line number = number - 100; after you push the number, so it will count the 1st 100, and stop before pushing the -75:
var number = 525;
var array = [];
while (number > 0) {
array.push(Math.min(number, 100));
number = number - 100; // remove number after pushing
}
console.log(array);
And a fancy one using Array#from:
const number = 525;
const get100s = (number) => Array.from({ length: Math.ceil(number / 100) }, (_, i) => {
const leftover = number - i * 100;
return leftover > 100 ? 100 : leftover;
});
console.log('525: ', get100s(525));
console.log('500: ', get100s(500));
console.log('10: ', get100s(10));
The problem here is that you are substracting 100 to the number after adding it to the array.
The code should be like this:
var number = 525;
var array = [];
while (number > 0) {
array.push(Math.min(number, 100));
number = number - 100;
}
Apparently I think completely different about this kind of problem. I would have divided the number by the section size and rounded down/truncated to get the number of sections, added that many sections to the final array, and then added the modulus of the section size and the number as the final value.
var value = 525;
function splitValue(val, sectionSize)
{
var sections = Math.floor(val/sectionSize);
var finalValue = val%sectionSize;
var splitValues = [];
for (var i = 0; i < sections; i++) {
splitValues.push(sectionSize);
}
splitValues.push(finalValue);
return splitValues;
}
var valueArray = splitValue(value, 100);
console.log(valueArray);
You can also use modulus.
let x = 556;
let mod = 100;
let res = [];
for (let i = 0; i < parseInt(x / mod); i++) res.push(mod);
res.push(x % mod);
I'm trying to iterate through a Redis bitmap and get the offset of all set bits.
I tried several methods but all of them have failed, here is a snipped I got from Redis Essentials book by Maxwell. I went through the code several times, still unable to figure out whats going wrong.
var bitmap = 'test';
redisClient.get(bitmap, function(err, bitmapValue){
var userIds = [];
var data = bitmapValue.toJSON().data;
data.forEach(function(byte, byteIndex){
for(var bitIndex = 7; bitIndex >= 0; bitIndex--) {
var visited = byte >> bitIndex & 1;
}
if(visited === 1) {
var userId = byteIndex * 8 + (7 - bitIndex);
userIds.push(userId);
}
})
console.log(userIds);
});
Most of the time I get empty result, usually when the bitmap is small. When I try large bitmaps I get array full of multiples of 8. A sample result I got is shown below, which is incorrect, the bitcount of that bitmap is 690, while I got only 91.
[8,
16,
32,
144,
176,
256,
320,
440,
456,
520,
584,
592,
624,
640,
648,
680,
696,
704,
712,
720,
752,
760,
848,
1056,
1088,
1104,
1112,
1120,
1136,
1144,
1160,
1168,
1224,
1248,
1264,
1280,
1312,
1328,
1424,
1480,
1496,
1544,
1600,
1680,
1792,
1824,
1840,
1904,
1976,
2080,
2112,
2144,
2224,
2240,
2296,
2392,
2400,
2504,
2512,
2584,
2616,
2712,
2856,
2880,
2912,
2976,
3048,
3288,
3328,
3352,
3360,
3368,
3376,
3432,
3440,
3496,
3600,
3616,
3624,
3632,
3704,
3752,
3776,
3832,
3880,
3936,
4056,
4264,
4296,
4352,
4376]
I don't have the bitmap you've stored under the key test, so testing is difficult. However, reading the code it looks like the if statement is placed wrongly - it should be:
var bitmap = 'test';
redisClient.get(bitmap, function(err, bitmapValue){
var userIds = [];
var data = bitmapValue.toJSON().data;
data.forEach(function(byte, byteIndex){
for(var bitIndex = 7; bitIndex >= 0; bitIndex--) {
var visited = byte >> bitIndex & 1;
if(visited === 1) {
var userId = byteIndex * 8 + (7 - bitIndex);
userIds.push(userId);
}
}
})
console.log(userIds);
});
I have a function that generates an array of values, like:
[343, 709, 88, 4, 0] = total of 1144.
The values generated by my function always add up to some set number nMax (in this case, 1144).
But now I separate function where I can pass an array of these values and fit them into a range determined by nMin to nMax, like refit(array, nMin, nMax).
Let's say nMin = 30, nMax = 1144.
For this example, that would mean I need 0 to become 30, and the rest of the numbers to proportionally get smaller, so that all of the numbers still add up to nMax (1144).
I tried creating a function based on a formula I found here (https://math.stackexchange.com/questions/43698/range-scaling-problem) but it's not working:
var oldMin = 0; //constant
var oldMax = 1144; //constant
function refit(oldArray, newMin, newMax) {
var newArray = Array(oldArray.length);
for(var i=0; i < oldArray.length; ++i) {
var oldValue = oldArray[i];
var newValue = (newMin * (1 - ((oldValue - oldMin) / (oldMax - oldMin)))) + (newMax * ((oldValue - oldMin) / (oldMax - oldMin)));
console.log(oldValue + ' = ' + newValue);
newArray[i] = newValue;
}
return newArray;
}
The output of this looks like:
343 = 364.00524475524475
709 = 720.4073426573426
88 = 115.6923076923077
4 = 33.89510489510489
0 = 30
Which is not correct, because the output adds up to 1264 which is not right.
Suggestions?
I got this a function eval in javascript - http://pastebin.com/E1PXQeKj
but i don't know how read it? how does this generate or decode the string?
or simply how is this code?
thanks for help!
Paste it in http://jsbeautifier.org/ and it reveals:
var secret = 'dDGSUW1QU01JaVZNTWFFN0pWcm2RZkE6MD';
To do it manually, find the end of the eval function and replace eval by alert or whatever debugging function you use. Line 2 can be handled in that way.
Line 3 formatted looks like:
var _9581
;
var _8438 = '196E84D180D984E928C920F980E928D988C652F768E652F680B924F808E924A724E916A872C1000A720A848E872F868D980E864C796E940A724B912F724B732B724B924C876C956D836C912C864A804E1008E840C868A800A740A832E848B680D760F';
var _5668 = /[\x41\x42\x43\x44\x45\x46]/;
var _9413 = 2;
var _9565 = _8438.charAt(_8438.length - 1);
var _5032;
var _9978 = _8438.split(_5668);
var _4678 = [String.fromCharCode, isNaN, parseInt, String];
_9978[1] = _4678[_9413 + 1](_4678[_9413](_9978[1]) / 21);
var _6432 = (_9413 == 7) ? String : eval;
_5032 = '';
_11 = _4678[_9413](_9978[0]) / _4678[_9413](_9978[1]);
for (_9581 = 3; _9581 < _11; _9581++) _5032 += (_4678[_9413 - 2]((_4678[_9413](_9978[_9581]) + _4678[_9413](_9978[2]) + _4678[_9413](_9978[1])) / _4678[_9413](_9978[1]) - _4678[_9413](_9978[2]) + _4678[_9413](_9978[1]) - 1));
_6432(_5032);
Now analyse it yourself. Care should be taken when something is executeable. Constructs like foo(bar).
Decoding in progress:
var _9581;
var _8438 = '196E84D180D984E928C920F980E928D988C652F768E652F680B924F808E924A724E916A872C1000A720A848E872F868D980E864C796E940A724B912F724B732B724B924C876C956D836C912C864A804E1008E840C868A800A740A832E848B680D760F';
var _5668 = /[ABCDEF]/;
var _9413 = 2;
var _9565 = "F";
var _5032;
var _9978 = [196, 84, 180, 984, 928, 920, 980, 928, 988, 652, 768, 652, 680, 924, 808, 924, 724, 916, 872, 1000, 720, 848, 872, 868, 980, 864, 796, 940, 724, 912, 724, 732, 724, 924, 876, 956, 836, 912, 864, 804, 1008, 840, 868, 800, 740, 832, 848, 680, 760, ];
var _4678 = [String.fromCharCode, isNaN, parseInt, String];
_9978[1] = "4"; //String(parseInt(84) / 21)
var _6432 = eval;
_5032 = '';
_11 = 49; //parseInt(196) / parseInt(4);
for (_9581 = 3; _9581 < _11; _9581++) {
//_5032 += (String.fromCharCode((parseInt(_9978[_9581]) + parseInt(180) + parseInt(4)) / parseInt(4) - parseInt(180) + parseInt(4) - 1));
_5032 += String.fromCharCode((parseInt(_9978[_9581]) + 184) / 4 - 177);
}
// so from here one, we can safely assume that the code is NOT executable
//_6432(_5032);
console.log(_5032);
Yields:
secret = 'dGd3bWw1QWVrUDh2a412dXl2aUFyOVE6MQ';
Conclusion:
The first packer is to confuse people who manage to decode it.
The second code actually changes the secret variable
Here is unpacker for code encoded in such a way http://www.strictly-software.com/unpacker. It seems the only thing this code do is:
ver secret = 'dGd3bWw1QWVrUDh1a242dXlNaUFyOVE6MQ';