While this question may seem a bit random, it actually does make sense - at least to me. Let's consider a multi-dimensional array named temporaryFrequency
. I want to create a function that takes one argument - frequencyArray[number]
. Let's have a look at the code for better understanding:
JavaScript:
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 function is designed to return the maximum frequency from a sub-array within an array. Now, let's run the following code to execute the function:
//Determine the maximum of the temporary frequencies
for(var n = 0; n < temporaryFrequency.length; n++){
var maximumTempFrequency + (n + 1) = getMaxTemporaryFrequency(temporaryFrequency[n]);
}
Now, here comes another query. Can we concatenate variable names as I did in the code above? For instance, creating variables like: maximumTempFrequency1
, maximumTempFrequency2
, maximumTempFrequency3
, and so forth.
This function call passes the nth element of the temporaryFrequency
array to the function, aiming to return the highest value from that particular sub-array. However, this approach seems to be flawed.
To summarize my inquiry:
1) Is it possible to pass an array with a key to a function, as demonstrated here? If not, is there an alternative method?
2) Can we concatenate strings to form a variable name, like in the 'for' loop? If not, is there a different approach?
*NOTE: The contents of the temporaryFrequency
array are not relevant to this question.