unable to get value of i in for loop - javascript

I have a simple for loop and it is matching condition according to my log files
but still the value of i is showing undefined
here is my javascript code
var indexs,i;
for(i = 0; i < data[0].length; i++){
if (new Date(data[0][i]) == new Date(inRecord.rdate))
{indexs = i;
}
Logger.log(new Date(data[0][i])+ " : " +new Date(inRecord.rdate) +": indexes:"+indexs)
}
and Image below if my log output which shows the condition should matched.
what am I doing wrong here?

new Date returns an object. And two objects can not be compared even where they contain the same data. The result will always be false.

new Date creates an object which cannot be compared. Even if two of these objects are identical, they will not produce a true comparison. To get around this, define them before the IF statement and compare the millsecond values with getTime(). This will be comparing two numbers (not objects) and work great :)
let dataDate = new Date(data[0][i])
let recordDate = new Date(inRecord.rdate)
if (getTime(dataDate) == getTime(recordDate)) {
indexs = i;
}
Happy to clarify if this doesn't work.

Related

Apps Script For Loop stuck at 0 in iteration although it is running

I have an array 'vnData' containing 5, 6 rows from where I am trying to extract 3rd column values (based on a criteria) and insert to a new array. Here is my code
for (odr = 0; odr < vnData.length; odr++){
Logger.log(vnData);
tempOdr = vnData[odr][3];
Logger.log(odr);
Logger.log(tempOdr);
Logger.log(vnData[odr][3]);
for(k = 0; k < vnData.length; k++){
if(vnData[k][3] = tempOdr){
odrVal = odrVal + vnData[k][11];
}
}
if(odrVal > 0){
affOdrSet.push(tempOdr);
}
Logger.log(affOdrSet);
}
Logger gives right value of odr in Logger.log(odr); but in Logger.log(vnData[odr][3]); I am always getting a result where value of odr is 0.
So for each iteration I get value from first row. Please help what is wrong in it.
One more thing, if I log Logger.log(vnData[3][3]) in place of Logger.log(vnData[odr][3]) then for first iteration it gives me right value from row 4 but for all subsequent iterations even Logger.log(vnData[3][3]) gives value from first row which is really weird.
The problem is the expression of the first if statement. Instead of
vnData[k][3] = tempOdr
use
vnData[k][3] === tempOdr
The above because = is the assign operator but it's very likely that instead of assigning tempOdr to vnData[k][3] what you want to compare them.

printing array shows different values when printing only the last element - javascript

im getting logs with a date property in the ISO YYYYMMDDhhmmss format, and im transfering them to Date object using the map function as seen below.
when I print the whole object I get the wrong dates but when I print just one element of the array its correct, this is bizarre to me,what am I missing ?
let dates = ascendingLogs.map(log => new Date(log['date'].replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$4:$5:$6 $2/$3/$1'
)));
console.log(dates[dates.length - 1])
console.log(dates)
let numberOfElements = 8
let timeGap = (dates[dates.length -1].getTime() - dates[0].getTime()) / numberOfElements;
let labels = []
for (let i = 1; i < numberOfElements; i ++){
dates[i] = new Date(dates[i - 1].getTime() + (timeGap));
};
below are the console.log of 1. last element in dates, whole dates array, ascendingLogs object
EDIT :
I have been able to narrow down the problem to the for loop, what im thinking is that it somehow asynchronously changes the console.log value but I dont know how to solve it.
heres a reproducible example you could examine :

Push string to array if the array you want to push from is undefined

I am not a coder, I am messing around with some JavaScript as part of modding a game, so bear with me. This game supports es5/everything Chromium 28 supported.
I had code which pushed a string to an array from a variable, and when the variable was undefined a fixed string was pushed instead:
slotsArray.push({
landing_policy: ai.landing_policy || 'no_restriction'
});
The setup changed such that where ai.landing_policy was set it would contain multiple values, so it become an array. When it wasn't set only a single entry was required.
The same code does not appear to work where an array is in place:
for (var i = 0; i < count; i++) {
slotsArray.push({
landing_policy: ai.landing_policy[i] || 'no_restriction'
});
}
An error is produced because it's trying to check a value from a variable that hasn't been defined. I expected that to cause it to use the fixed value, but apparently that's not what happens, it just fails.
I've changed my approach to the code seen below in full:
if (Array.isArray(ai.landing_policy)) {
for (var i = 0; i < count; i++) {
slotsArray.push({
landing_policy: ai.landing_policy[i]
});
}
}
else {
slotsArray.push({
landing_policy: ai.landing_policy || 'no_restriction'
});
}
This code works, but what I'm looking to understand is whether this was the best solution? The old method felt elegant, while the new one looks a little clumsy.
You can use the ternary operator(? :).
It will return the second value if the first is true, and the third otherwise.
I've used array instanceof Array instead of Array.isArray(array) to support ES5.
var isArray = ai.landing_policy instanceof Array
for (var i = 0; i < (isArray ? count : 1); i++) {
slotsArray.push({
landing_policy: isArray ? ai.landing_policy[i] : ai.landing_policy || 'no_restriction'
});
}
Elegant solution not always converse to the most readable/desirable. I would probably do something like:
const formattedPolicy = ai.landing_policy.map(policy => policy || 'no_restriction');
slotsArray = [...formattedPolicy ];
Course this has to imply that the ai.landing_policy is always an array. If you need to double check first you could also do:
const formattedPollicy = ai.landing_policy.constructor === Array
? ai.landing_policy.map(policy => policy || 'no_restriction');
: [ai.landing_policy]
Looks like an elegant or short imho but your code is way more readable.

When iterating through array, first value is always "undefined"

I created a quiz and I'm trying to iterate through the elements in the HTML form (radio buttons), adding the value of the button to a string if it is checked. However, the first thing added to the string is always "undefined" and I can't work out why. Please note, I'm very new to Javascript.
I've tried changing the way the for loop works - using (inputs.length+1), or setting i to 1 instead of 0. This doesn't seem to be the issue however. I've also tried checking that the value isn't undefined before adding it to the string (as shown below), but it still results in the first part of the string saying "undefined".
var chosen_result; //the string to add values to
var temp;
var inputs = document.forms["townquiz"].elements;
for (i = 0; i < (inputs.length-1); i++) {
temp = inputs[i];
if((temp.checked) && !(temp.value == "undefined")){
chosen_result += temp.value;
Actual Result: undefinedABC
Expected Result: ABC
where A, B and C are values of the radio buttons in the HTML form.
In regards to strings, the += operator concatenates the current value and a new value. chosen_result is undefined because it was declared but not initialized. Simply set chosen_result to an empty string:
var chosen_result = ""; //the string to add values to
var temp;
var inputs = document.forms["townquiz"].elements;
for (i = 0; i < (inputs.length-1); i++) {
temp = inputs[i];
if((temp.checked) && !(temp.value == "undefined")){
chosen_result += temp.value;
Assign an empty string to chosen_result like chosen_result = '' to avoid the value being undefined when you append to it.
String addition in JavaScript can be somewhat strange, because it uses coercion. When you declare chosen_result, you're not setting it to anything, so its type is undefined. When you add a string to undefined, JavaScript will turn it into a string, then concatenate it. String(undefined), however, is "undefined", so, what you're really doing is
"undefined" += "ABC"
To fix this problem, initialize chosen_result as an empty string:
var chosen_result = ""
This way, it won't be coerced.

How to pass an array with a key to the argument of a function? (Working with multi-dimensional arrays)

This may not be a good question, but it does makes sense(atleast to me, it does.)
Suppose I have a multi-dimensional array called temporaryFrequency. Now, I want to write a function which will take one argument --> frequencyArray[number]. This will be more clear from the code:
JS:
function getMaxTemporaryFrequency(frequencyArray[number]){
var maxOutofThese = frequencyArray[number][0];
for(var i = 0; i < frequencyArray[number].length; i++){
if(frequencyArray[number][i] > maxOutofThese)
maxOutofThese = frequencyArray[number][i];
}
return maxOutofThese;
}
This is the function which will return the maximum frequency from a sub-array of an array. Now, I will execute the following code to call the function:
//This is to get the max out of the temporary frequencies
for(var n = 0; n < temporaryFrequency.length; n++){
var maximumTempFrequency + (n + 1) = getMaxTemporaryFrequency(temporaryFrequency[n]);
}
Now, I have one more question attached to this. Can variable names be concatenated, like a did here? For example, for each loop count, I want to make variables : maximumTempFrequency1 , maximumTempFrequency2 , maximumTempFrequency3 and so on.
Now, this call of the function passes the nth element of the temporaryFrequency array to the function, which should return the greatest value from that nth sub-array. But, that is not working.
So, to summarise my question:
1) Can we pass an array with a key to function, like I did here? If
no, is there any way to do it?
2) Can we concatenate strings to make a variable name, like I did in
the 'for' loop? If no, is there any other method
*NOTE: For this question, viewers don't need to know the contents of the temporaryFrequency array.
The straight answer to your two questions are No.
The syntax function getMaxTemporaryFrequency(frequencyArray[number]) { ... } is not valid. The parser is expecting an identifier for the parameter name, and identifiers can't have brackets in them [ or ]. This results in the error:
Uncaught SyntaxError: Unexpected token [
Similarly, your variable name is also syntactically invalid. var maximumTempFrequency + (n + 1) = //anything results in
Uncaught SyntaxError: Unexpected token +
To answer your follow up questions of How to make it work, for the first question you can pass the dereferenced array member to the function when you call it (instead of when you declare it).
var frequencyArray = []; // fill it with your values
var number = 0; // or whatever number you want
function getMaxTemporaryFrequency(arr){
var maxOutofThese = arr[0];
for(var i = 0; i < arr.length; i++){
if(arr[i] > maxOutofThese)
maxOutofThese = arr[i];
}
return maxOutofThese;
}
function getMaxTemporaryFrequency(frequencyArray[number]);
For the latter one, you want to store the results to an array, like this:
//This is to get the max out of the temporary frequencies
var maximumTempFrequency = [];
for(var n = 0; n < temporaryFrequency.length; n++){
maximumTempFrequency[n+1] = getMaxTemporaryFrequency(temporaryFrequency[n]);
}
Note that your syntax getMaxTemporaryFrequency(temporaryFrequency[n]) is an example of what I put in the first code block above.

Categories

Resources