I am writing a program that will take the contents of a text area, pull out the important info, format it and then output it.
I have successfully made my program pass all the info into an array.
Currently I am working on getting the program to identify where the customer’s name is located (it will always be between “Name” and either “Details” or “Customer” if “Details” is not in the correct location in the array.
// Parse the input box into an array
var inputArr = document.getElementById("inputBox").value.split(/[\s]/);
// Instantiate variables
var custDet, contactName, phone, companyName, email;
// Pull out all strings between "Name" and "Email" ("Card" should always be after "Email" or else it's the wrong string
if(inputArr[inputArr.indexOf("Email") + 1] == "Card") {
custDet = inputArr.slice(inputArr.indexOf("Name") + 1, inputArr.indexOf("Email"));
// Pass the customer's name into the string contactName
for(i = 0; custDet[i] != "Details" || custDet[i] != "Customer" || i < custDet.length; i++) {
if(custDet[i].search(",") != -1) {
var temp = custDet[i].split(/[,]/);
temp.reverse();
contactName = contactName + temp.join(" ");
}
}
contactName = contactName + custDet.join(" ");
} else {
contactName = prompt("Error: Could not locate a valid Contact Name. Please input Contact Name.");
phone = prompt("Error: Could not locate a valid Contact Phone Number. Please input Contact Phone Number.");
companyName = prompt("Error: Could not locate a valid Company Name. Please input Company Name.");
email = prompt("Error: Could not locate a valid Email Address. Please input Email Address.");
}
The error is being thrown on…
if(custDet[i].search(",") != -1) {
And I don’t understand why. Any help with my logic would also be appreciated.
Thank you all. 🙂
That error likely means that you tried to reference item
iofcustDet, butcustDetdidn’t have that many elements in it.Your for loop is “non-standard”:
and I suspect that’s the source of the problem.
ibecomes higher thancustDet.length, so that meanscustDet[i]returns undefined. Sinceundefined != "Details"is true, the loop keeps going pastcustDet.length.