0

I am trying to print the array with the two variables in it, but it keeps loging "undefined" What am I doing wrong?

var firstNum = 1
var secondNum = 2
var list = []
var i;
    function calculate(firstNum,secondNum) {
        for( var i = firstNum; i <= secondNum; i++){
            list.push(i);

        }
        console.log(list);
};

The function is called in the form tag:

<form name="myForm" id="eForm" onsubmit = "calculate()">

This is a JS Bin with the form and js: http://jsbin.com/EcOtUJIx/1/

Henry Lynx
  • 1,099
  • 1
  • 19
  • 41
  • 2
    Where/how are you actually *calling* `calculate()`? – DCoder Jan 20 '14 at 06:11
  • Are you saying that `console.log(list);` logs `undefined`? – Felix Kling Jan 20 '14 at 06:14
  • in the form tag with
    – Henry Lynx Jan 20 '14 at 06:15
  • 2
    Variable and function declarations all evaluate to `undefined` (and can't be used as rvalues anyway), so there you go. – Dagg Nabbit Jan 20 '14 at 06:16
  • @Dagg: Can you explain your comment better? – Felix Kling Jan 20 '14 at 06:18
  • @FelixKling, IDK, maybe. Was it unclear? – Dagg Nabbit Jan 20 '14 at 06:18
  • 1
    @Dagg: How is your comment related to the question? The OP tries to log `list` not a variable declaration or a function definition. And he doesn't try to use them as an rvalue either. – Felix Kling Jan 20 '14 at 06:20
  • 2
    You're calling the function with no arguments. – omninonsense Jan 20 '14 at 06:20
  • 1
    @FelixKling, he asked why the code evaluates to undefined; the reason is because no statement in that code evaluates to anything. If it included a statement that did evaluate to something, the entire program would evaluate to whatever the last statement evaluated to. – Dagg Nabbit Jan 20 '14 at 06:22
  • Ah, you are referring to the title. He also says: *"but it keeps loging "undefined""*. – Felix Kling Jan 20 '14 at 06:22
  • 1
    It "works fine" for me: http://jsfiddle.net/HLJ4u/. It logs an empty array, not `undefined`. That's surely not what you want, but it also isn't the problem you describe. So please provide more information so that we will be able to actually reproduce the problem. – Felix Kling Jan 20 '14 at 06:23
  • @FelixKling, the console just spits out whatever the program evaluates to, so it seemed relevant to me. The code in your example still does not evaluate to anything, it just manually dumps something in the console (not the same as the value the console gives from evaluating the program when it's typed into the console). – Dagg Nabbit Jan 20 '14 at 06:26
  • @Dagg: If the code was typed in the console, then yes, your comment would be relevant. But since the OP is calling the function from an inline event handler, we can assume that the code is actually included inside a script tag. And in this context I think your comment is confusing rather than helping. – Felix Kling Jan 20 '14 at 06:28
  • @FelixKling aha, I just noticed the comment with the event handler. Didn't catch that before; should probably be part of the question. – Dagg Nabbit Jan 20 '14 at 06:30
  • @DaggNabbit - Yes, I saw that after commenting - missed it at first because the comments weren't all expanded. Meanwhile I deleted my own comment and turned it into an answer. – nnnnnn Jan 20 '14 at 06:38
  • After your edit, where exactly do you see `undefined`? If I click submit, the form is simply submitted. – Felix Kling Jan 20 '14 at 06:40
  • It is submitted yes, but it doesn't log the array in the console with the values from the form. – Henry Lynx Jan 20 '14 at 06:45
  • @HenryLynx most browsers clear the console when the page navigates – Dagg Nabbit Jan 20 '14 at 06:46
  • @HenryLynx: But it also isn't what you described in your question. If you don't explain the actual problem you're facing, then it's difficult for us to help you. – Felix Kling Jan 20 '14 at 06:57
  • @Felix Kling: I understand, but you have answered many other questions. Thanks! – Henry Lynx Jan 20 '14 at 07:24

2 Answers2

2

According to your comment, and updated question, you are calling the function from a form's inline event handler:

onsubmit = "calculate()"

You don't pass any arguments to the function, which means within the function both firstNum and secondNum are undefined. I think you are confusing them with your global variables of the same name.

If you changed the function declaration to remove the parameters:

function calculate() {

...then within the function when you reference firstNum and secondNum it would use the global variables. (Or you could change the parameter names, but since you don't actually pass any parameters in it makes more sense to remove them.)

Demo: http://jsfiddle.net/5WBB5/

Note that it is normal practice to declare variables that are only used within a function inside that function:

   function calculate() {
       var firstNum = 1;
       var secondNum = 2;
       var list = [];
       for (var i = firstNum; i <= secondNum; i++) {
           list.push(i);
       }
       console.log(list);
   };

(Notice that that has the same output: http://jsfiddle.net/5WBB5/1/)

If you need the function to make use of different functions then you declare parameters as in your code but pass values in when you call the function:

function calculate(firstNum, secondNum) {
    // your code here
}

calculate(1, 2);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

call the function after the definition:

calculate(firstNum, secondNum); // works as calculate(1, 2);

then it will display the array in console

ReNiSh AR
  • 2,782
  • 2
  • 30
  • 42
  • I think you're right, but the call should probably be `calculate(firstNum, secondNum) ` to make use of the global variables. Or the formal parameters in the function declaration can be omitted altogether so that the globals are used directly. – RobG Jan 20 '14 at 06:24
  • Oh, and the call only needs to be after the variable declarations, it can be before the function declaration. – RobG Jan 20 '14 at 06:26