I’m trying to reuse the same variable in a second if statement from the first if statement and I get the warning that my variable is undefined when I throw an alert on it.
How could I make this variable retain its value on the second if statement? I’m only copying part of the code for you to see what I’m talking about. I am trying to use foundRangeStart in the second if statement for ESNEnd, which was previously used in the first if statement for ESNStart.
Why is foundRangeStart set to undefined in the second if statement?
if ($(this).attr("id") == "ESNStart") {
var esnStart = [];
esnStart[0] = esn_to_num(enteredData);
var item;
var rangesFoundStart = [];
rangesFoundStart[0] = -1; // Set defaut value as -1 for the corresponding entry in the "rangesFound" array
for (var x in ranges) { // Inner loop over items in ranges object
item = esnStart[0];
if (item >= ranges[x].min && item <= ranges[x].max) {
rangesFoundStart[0] = x;
}
}
if (rangesFoundStart[0] >= 0) {
foundRangeStart = rangesFoundStart[0];
}
if (foundRangeStart && foundRangeStart >= 0) {
$("#STxModel").val(ranges[foundRangeStart].StxName);
$("#TxSpacing").val(ranges[foundRangeStart].rtumodel);
}
}
if ($(this).attr("id") == "ESNEnd") {
var esnEnd = [];
esnEnd[0] = esn_to_num(enteredData);
var item;
var foundRangeEnd;
var rangesFound = [];
rangesFound[0] = -1; // Set defaut value as -1 for the corresponding entry in the rangesFound array
for (var x in ranges) { // Inner loop over items in ranges object
item = esnEnd[0];
if (item >= ranges[x].min && item <= ranges[x].max) {
if (rangesFound[x] == foundRangeStart) {
rangesFound[0] = x;
var foundone = rangesFound[0] = x;
alert(foundone + foundRangeStart);
}
} else {
alert(alert_total);
}
}
}
Because in that control path,
foundRangeStarthas not been set.It only appears to be set here, in the first
ESNStartblock;But that line is not executed if this statement does not equal
true;