I am making a project where I need to read in numbers that can be in different format and check these against the users input. In some cases this will be an intervall like "8800-9000", in some cases it will be a 4-digit number, in some cases a 5-digit number.
The array looks like this:
var testArray = [
{
"label": "Nordea",
"value": ["1100-1199", "1400-2099", "3000-3399", "3410-4999"]
},
{
"label": "Swedbank",
"value": ["7000-7120", "7123-8104", "8106-8999"]
},
{
"label": "Sparbanken Nord",
"value": "8264"
},
{
"label": "Sparbanken i Enköping",
"value": ["7121-7122", "8305-5"]
}];
I've made a huge nested if-mess that works kind of OK. My only issue is that its not breaking when it finds a match. If i input "8305-5" it will find a match in 2 different object property arrays. Have I designed the "between"-function wrong?
Also I can search for numbers that are way bigger than the intervalls and still get a match. Intervall "3410 - 3999" is in an array, and if I search for "39999" it will still get a match.
All help much appreciated!
I iterate over all objects in the array, and if the value-property is an array I will check the lenght of each element to see if I should match a "between"-value or a straight "===" match.
If its not an array I try a simple match.
function searchForClearing() {
var userInput = document.getElementById("clearingnummerTextBox").value;
for (var i in testArray) {
var currentObject = testArray[i];
if (Array.isArray(currentObject.value)) {
for (i = 0; i < currentObject.value.length; i++) {
if (currentObject.value[i].length === 9) {
var firstInterval = currentObject.value[i].split('-')[0];
var lastInterval = currentObject.value[i].split('-')[1];
if (userInput >= firstInterval && userInput <= lastInterval) {
console.log("Inmatat: " + userInput + " " + "Träff på: " + currentObject.value);
document.getElementById("bankResult").innerHTML = currentObject.label;
console.log("Sökte mellan intervallen: " + firstInterval + " - " + lastInterval);
console.log("9 teckens sök");
}
} else if (currentObject.value[i].length === 6) {
if (userInput == currentObject.value[i]) {
console.log("Inmatat: " + userInput + " " + "Träff på: " + currentObject.value);
document.getElementById("bankResult").innerHTML = currentObject.label;
console.log("Sökte mellan intervallen: " + firstInterval + " - " + lastInterval);
console.log("6 teckens sök");
}
}
}
} else {
if (currentObject.value.length === 9) {
var firstInterval = currentObject.value.split('-')[0];
var lastInterval = currentObject.value.split('-')[1];
if (userInput >= firstInterval && userInput <= lastInterval) {
console.log("Inmatat: " + userInput + " " + "Träff på: " + currentObject.label);
document.getElementById("bankResult").innerHTML = currentObject.label;
console.log("Sökte mellan intervallen: " + firstInterval + " - " + lastInterval);
return true;
}
} else if (currentObject.value.length === 4) {
if (userInput == currentObject.value) {
console.log("Träff på clearingnummer som inte var i en array: " + userInput + " " + currentObject.label);
document.getElementById("bankResult").innerHTML = currentObject.label;
return true;
}
}
}
}
}