I am not able to understand the code - javascript

I am new to javascript. After lots of hours I am not able to understand the code. I would be grateful if someone could help me. Thanks
for(var x = 0; x < id_inters['inters'].length; x++) {
var a,b;
if(id_inters['inters'][x]['First_ID'] == data.main[0].cust) {
a = id_inters['inters'][x]['First_ID'];
b = id_inters['inters'][x]['Second_ID2'];

You can take it with these steps from the for loop:
var x is declared and assigned value of 0.
There will be an Object named id_inters which hold an array named inters which also holds objects in it.
Now inside the loop you declare two variables var a, b;.
Now that n the condition you are checking the value of the iterated object with id_inters['inters'][x]['First_Id'] where x is the index number if each iteration which is incremented from 0 to length of array.
And you are checking each iterated value to the first value of the data['main'][0].cust. Where data is an object which contains an array named data and you are comparing its first object's cust value.

You are given an array A of size N.
A partitioning of the array A is the splitting of A into one or more non-empty contiguous subarrays such that each element of A belongs to exactly one of these subarrays.
Find the number of ways to partition A such that the parity of the sum of elements within the subarrays is alternating. In other words, if Si denotes the sum of the elements in the i-th subarray, then either
S1 is odd, S2 is even, S3 is odd and so on.
or S1 is even, S2 is odd, S3 is even and so on.
For example if A=[1,2,3,3,5]. One way to partition A is [1,2][3,3][5]. Another way to partition A is [1][2][3][3,5]. Note that there exists more ways to partition this array.
Since the answer may be large, output it modulo 998244353.
Input Format
The first line contains a single integer T - the number of test cases. Then the test cases follow.
The first line of each test case contains an integer N - the size of the array A.
The second line of each test case contains N space-separated integers A1,A2,…,AN denoting the array A.
Output Format
For each test case, output the answer modulo 998244353.
Constraints
1≤T≤10000
1≤N≤2⋅105
0≤Ai≤109
Sum of N over all test cases does not exceed 2⋅105
Sample Input 1
3
3
1 2 3
4
4 4 4 4
5
1 2 3 3 5
Sample Output 1
2
1
5
Explanation
Test case 1: The array can be partitioned as follows
[1][2][3]
[1,2,3]
Test case 2: The array can be partitioned as follows
[4,4,4,4]

Related

How array of 2 has length 2?

As it's common arrays in js if you put 3 elements in array it will has length 3 but if I make array with only number two, it has length 2 ?!
const myArry=new Array(2)
console.log(myArry.length) // 2
new Array(2) means "make me an array with two empty slots". So yes, it has length 2.
You may be looking for const myArry = [2] which has length 1.
EDIT
Just for fun, if you really wanted to use constructor syntax instead of array literal syntax (the [2] part), you could do:
new Array(1).fill(2)
Which actually could make sense if both the length and the fill value were dynamic parameters.

Find k number of array elements having the minimum difference amongst themselves

So basically I have an array of n integers (positive only). I want to have k number of integers from this array into a separate array (k<n) such that the difference between these k numbers is the minimum amongst every other k pairs of integers in an array.
If k is 1, I just need to return the max integer of the array.
I want to implement this in JavaScript. I understand how to run this problem for the values k=1 and k=2. But I don't grasp the general concept of this problem.
For eg:
Array = [6,22,21,63,99,77]
I sorted this array in ascending order. After this I don't understand how to proceed further.
After sorting the array it becomes similar to a sliding window problem.
Run a loop from i=0 to n-k and check the following.
Find the minimum difference between arr[i+k] and arr[i]. The index at which this occurs is your subset of k integers you want.
You can check this link for more details and coding help.

Set value to an array in pentaho javascript step

i have some values in the input and i want to save them into an array but first i want to set the "0" index of the array to 0 and then in the 1,2,3,4 indexs add the value of the input but i can get to make it
before to set the values of the input i set the 0 to the position 0 of the array but it doesnt work
the thing is that i want to use only the first four number of the input_data into an array but first set the 0 position to 0 and then add 5,10,15,20.
so the final array would be 0,5,10,15,20
i set the index "0" of the array to 0 and the other indexes i fill them whit the incoming data.
This depends on how the data is reaching the 'Modified Java Script Value' step.
If it is one single row, with the string containing a "CSV", then Rohit.007 answer will suffice.
If you have multiple rows reaching the step, the Script will Repeat N(row) times. If you have 4 rows being fed to the step, this script will run 4 times, so you need some sort of restraint on the Variables, so you don't repeat some parts of the code.
Personally i would use something like this for Pentaho:
I generated 4 rows, with an add sequence , 1 to 4.
The first run of the script creates the array, pushes 0 and the value of the first row. The other iterations of the script just keep pushing whatever values are found on the specified row to this array (without "Re-declaring" it).
Remenber that the "For Each" command is kind of blurred in pentaho, since you're almost always dealing with multi-row tables, so whatever you do in scripts has to have some constraints on variables declarations.
You can try the below code.
let array = [];
array.push(0);
let string = '1,2,3,4';
array = array.concat(string.split(','));
let result = array.map(function (x) {
return parseInt(x, 10);
});
console.log(result);

What is an efficient way to combine 2 numbers to use as the key of an object?

I am using the following pattern to index an injection from pairs of numbers to numbers:
var myHash = {};
...
for (... billion of iterations ...)
var x = someNum;
var y = otherNum;
myHash[x + "," + y] = z;
The problem with this code is that I'm using a string as the key of myHash, which has been tested to be much slower than integer keys. My question is: what is a more intelligent way to combine 2 numbers before using them as keys of an object? I.E., how to combine 2 doubles into an unique Integer?
There is the definition of an array in JavaScript:
Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232 - 1. A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 232. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.
In other words, if the index you specify is a number representing an integer between 0 and 0xFFFFFFFE, then it is used as an array index. Any other value is taken as a string and it is used to create an object member instead of an array item.
So if you have constraints on your indices which would fit the valid range (0 to 0xFFFFFFFE) then you're good. Otherwise, what you have is probably the fastest.
So the following represents string indices which are members of object myHash:
myHash[x + "," + y] = z;
Someone mentioned using an array of arrays. That would not help you. You'd get many arrays instead of many strings. It would probably be about the same if not slower. The idea is something like this:
myHash[x] = []; // initialize the sub-array (must be done only once per value of 'x'
myHash[x][y] = z; // save z in that array
I do not recommend the double array because it will initialize one array for each value of 'x' on top of myHash and that probably not any faster than having the string concatenation (especially because you'll have to test whether the myHash[x] array was already defined or not...).
So... it is possible to write:
myHash[3.3] = "that worked?";
But if after that you check out the length, you'll notice it is zero:
console.log("Hash length = " + myHash.length);
This is because 3.3 is not an integer.

How does JavaScript [] really work?

I'm writing a JavaScript interpreter for extremely resource-constrained embedded devices (http://www.espruino.com), and every time I think I have implemented some bit of JavaScript correctly I realise I am wrong.
My question now is about []. How would you implement one of the most basic bits of JavaScript correctly?
I've looked through the JavaScript spec and maybe I haven't found the right bit, but I can't find a useful answer.
I had previously assumed that you effectively had two 'maps' - one for integers, and one for strings. And the array length was the value of the highest integer plus one. However this seems wrong, according to jsconsole on chrome:
var a = [];
a[5] = 42;
a["5"]; // 42
a.length; // 6
but also:
var a = [];
a["5"] = 42;
a[5]; // 42
a.length; // 6
So... great - everything is converted into a string, and the highest valued string that represents an integer is used (plus one) to get the length? Wrong.
var a = [];
a["05"] = 42;
a.length; // 0
"05" is a valid integer - even in Octal. So why does it not affect the length?
Do you have to convert the string to an integer, and then check that when converted back to a string, it matches?
Does anyone have a reference to the exact algorithm used to store and get items in an array or object? It seems like it should be very simple, but it looks like it actually isn't!
As the specs said, and was noted by others:
"A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32-1."
That's explain why in your scenario "5" is considered an array index and "05" is not:
console.log("5" === String("5" >>> 0));
// true, "5" is equal to "5", so it's an index
console.log("05" === String("05" >>> 0));
// false, "05" is not equal to "5", so it's not an index
Note: the Zero-fill right shift is the shortest way in JS to have a substitute of ToUint32, shifting a number by zero.
See MDN
It's possible to quote the JavaScript array indexes as well (e.g.,
years["2"] instead of years[2]), although it's not necessary. The 2 in
years[2] eventually gets coerced into a string by the JavaScript
engine, anyway, through an implicit toString conversion. It is for
this reason that "2" and "02" would refer to two different slots on
the years object and the following example logs true:
console.log(years["2"] != years["02"]);
So with a["5"] you are accessing the array while a["05"] sets a property on the array object.
Arrays are just objects. That means they can have additional properties which are not considered elements of the array.
If the square bracket argument is an integer, it uses it to perform an assignment to the array. Otherwise, it treats it as a string and stores it as a property on the array object.
Edit based on delnan's comment and DCoder's comment, this is how JavaScript determines if it is an appropriate index for an array (versus just a property):
http://www.ecma-international.org/ecma-262/5.1/#sec-15.4
Arrays are also objects.
By doing this
a["05"] = 5;
You are doing the same thing as:
a.05 = 5;
However, the above will result in a syntax error, as a property specified after a dot cannot start with a number.
So if you do this:
a = [];
a["05"] = 5;
you still have an empty array, but the property of a named 05 has the value 5.
The number x is an array index if and only if ToString(ToUint32(x)) is equal to x (so in case of "05" that requirement is not met).

Categories

Resources