I have a text box that will have a large block of text pasted into it.
Then I parse the text into an array, removing whitespace. I need to pull the data out of certain array elements and put them into separate variables so I can generate a cleaner, formatted output.
The problem is that noting appears to be passed into my variables from the array. I’ve toyed with it a bit, and the array is being filled correctly, but the elements aren’t passing strings to the variable.
HTML:
<p>Contact Name: <b id='contactNameOutput'></b></p>
JavaScript:
function generateOutputfvoc() {
var inputArr = document.getElementById('inputBox').value.split(/[\s]/);
document.getElementById('contactNameOutput').innerHTML = inputArr[0];
}
There are quite a few things going on here wich are hard to understand why you’ve done them.
My asumption is that
inputBoxis either a textarea or a input field into wich the user writes something.Okay so you’re splitting that string on whitespaces so for example the string
would result in a array which looks like this:
Now in your for-loop you’re iterating over this array, starting at 0 and incrementing till you reach the end of the array, nothing strange here.
But in the first iteration, in the if-clause that you have, you’re trying to access the array with negative values, remember
iis0, this results in an undefined value, also in the last part of the if clause you’re trying to accessi+1well what happens wheniis at its last value?! You guessed it another undefined! Later the loop will access old values which you’ve allready gone through, and honestly I can’t figure out what you’re trying to accomplish.All your if-statements are a mess really, especially if your gonna do them in that loop.
My guess is that you thought that you needed a for-loop for this, when in fact you do not.
What you have is one array containing the words in your textarea, if you know the order of what you’re expecting, (can you really know what the user will put in that textarea?), then you can just access the words directly since you’ll know the index of each item in the array. Otherwise, rewrite this to something simpler, always try to keep it simple.
Also why not look up
refactoringwhile you’re at it?!edit
I think that the problem you have now is that the first element in the array is an empty string, that happens if there’s a space in the beginning of the input.