3

In a simple script that goes through some form fields, gets the user input and stores it into arrays I ran into a problem.

It works when I do this:

var q1A = parseFloat($('#q1-1').val());
if(isNaN(q1A)) {
    var q1A = 0;
}
parameter.push(' ');
answers.push(q1A);

But now I added another array which, in this case, is supposed to simply store the same q1A variable. But somehow I end up with an "Uncaught TypeError" stating that the variable is undefined! The new code block is:

var q1A = parseFloat($('#q1-1').val());
if(isNaN(q1A)) {
    var q1A = 0;
}
input.push(q1A);
parameter.push(' ');
answers.push(q1A);

I logged the variable in the console and it works just fine, it's set and has a value. Any idea why it says it's undefined? The 'answers' array stores the value just fine.

Thanks in advance!

EDIT:

Of course I defined the variables. I just didn't post that part of the code...

var groups = new Array();
var questions = new Array();
var input = new Array();
var parameter = new Array();
var answers = new Array();
var amounts = new Array();
6
  • 1
    input is not an array, you can't push to any variable, only to an array that has been previously defined. Commented Aug 9, 2012 at 11:44
  • I defined those. Added it to the post now. Commented Aug 9, 2012 at 11:46
  • 1
    Works perfectly fine for me: jsfiddle.net/mP6NT/1. If we cannot replicate the error, it's very difficult for us to help you. Please create your own jsfiddle.net demo which shows this problem. Commented Aug 9, 2012 at 11:47
  • Alright, thanks. I will try to recreate the error there and come back to you guys! Commented Aug 9, 2012 at 11:52
  • Why are you redeclaring the variable q1A inside the if block? Commented Aug 9, 2012 at 13:17

2 Answers 2

5

Since it's possible to push an undefined variable into an array without error, the problem must be that input isn't defined when you try to call push() on it.

The usual reason for this problem is that there is another place where the variable input is declared and it's never initialized in the unexpected place.

Sign up to request clarification or add additional context in comments.

4 Comments

It's about the input array, answers works fine. But regardless of which one it is, everything works fine if I remove the input array. But when I add it, the function stops as soon as it gets to the first push into that array. I only declared the arrays once, at the start of the function. I am trying to recreate the problem in jsfiddle.
Search your code for var input; I'm sure you'll find at least one result that you don't expect.
Ok, nevermind. You were right after all. Thanks! I did however declare input again after the point where the error occurred.
var has function scope; there is no block level scope in JavaScript. See stackoverflow.com/questions/500431/javascript-variable-scope
4

First you have to define as array.

var   input=[];

then try to push the element.

input.push(element); 

1 Comment

I already declared the arrays, just didn't post that part of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.