0

I have written some Javacript code that calculates the lineal footage of multiple values that are typed in by the user. Here is the Javascript code...

var totalLinealFootage = 0;
for (var i = 0; i <= 24; ++i) 
{
    var p = +document.getElementById('pf' + i).value;
    var f = +document.getElementById('ff' + i).value;
    var i = (+document.getElementById('if' + i).value)/12;

    if (!isNaN(p) && !isNaN(f) && !isNaN(i)) 
    {
        totalLinealFootage += (f+i)*p;
    }
}

As of now no matter what values I place in the inputs totalLinealFootage has no value?

AB Bryan
  • 73
  • 1
  • 1
  • 6
  • Where is this in relation to the rest of the page? If it's just at the top in a script tag, you may need to move it to the bottom or wait until the page is loaded, then execute it. – teynon Jun 07 '13 at 17:44
  • 3
    That looks fine, **provided** that: 1. The elements in question are `input` elements. 2. They really have `id` attributes like `"pf0"`, `"pf1"`, etc. 3. The code accessing the elements isn't running until *after* the elements already exist (which seems likely given you're probably running this in response to a click or something, but...). (Also, JavaScript doesn't have block scope, so those vars are really at the function level, but it's harmless in the above.) – T.J. Crowder Jun 07 '13 at 17:46
  • 4
    i is already defined as the variable for the loop change the name inside the loop – Loris Jun 07 '13 at 17:49
  • use console.log() inside the loop: one NaN/null/undefined will sabotage your whole arithmetic. – dandavis Jun 07 '13 at 17:49
  • 2
    @Loris: That's an answer (and: good eye!), not a comment. :-) – T.J. Crowder Jun 07 '13 at 17:49
  • Yeah this is within a function that is called on a click, for some reason it just isn't working? – AB Bryan Jun 07 '13 at 17:50
  • can you post an example of using the .log() – AB Bryan Jun 07 '13 at 17:50
  • @ABBryan: Your browser has a debugger built into it (look for "Dev Tools" or similar), unless you're using something really old. You can step through the code, inspect the content of variables, etc., etc. – T.J. Crowder Jun 07 '13 at 17:51
  • Where do I find the Dev Tools? – AB Bryan Jun 07 '13 at 17:52
  • When I run the debugger it is crashing the browser? – AB Bryan Jun 07 '13 at 17:55
  • @ABBryan `console.log("Whatever you want to log");` – pinkpanther Jun 07 '13 at 17:56
  • can anybody tell me what those `+` in `+document.getElem...` supposed ? – pinkpanther Jun 07 '13 at 18:01
  • 2
    @pinkpanther String-to-number coercion: [Whats the significant use of Unary Plus and Minus operators?](http://stackoverflow.com/questions/5450076/whats-the-significant-use-of-unary-plus-and-minus-operators) – apsillers Jun 07 '13 at 18:02
  • @apsillers thanks for your response... – pinkpanther Jun 07 '13 at 18:03
  • @ABBryan If no one has solved your issue thus far, can you post the complete function and where you are referencing the value that is empty? – teynon Jun 07 '13 at 18:13

2 Answers2

3

I assume your reuse of i is tripping up your loop. At the end of the loop, when for handles the i, it may have completely changed value.

If var i = (+document.getElementById('if' + i).value)/12; sets i to some value greater than 24, the loop will terminate at the start of the next iteration due to the i <= 24 condition.

Instead, use a different variable name for your loop index and the values in your computation.

apsillers
  • 101,930
  • 15
  • 206
  • 224
  • Thank you I just changed the i to x and it works perfect. Cant believe I did not catch this, thank you. – AB Bryan Jun 07 '13 at 18:56
0

Markup:

<input id="pf0" value="1" />
<input id="ff0" value="2" />
<input id="if0" value="3" />

JavaScript:

var totalLinealFootage = 0,i,p,f,j;
for (i = 0; i < 1; i+=1) {
    p = document.getElementById('pf' + i).value;
    f = document.getElementById('ff' + i).value;
    j = (document.getElementById('if' + i).value)/12;
    totalLinealFootage += ((f+j)*p);
    console.log(totalLinealFootage);
}

This worked for me. Like @T.J Crowder said you need to make sure the you're using inputs and that you have all the inputs from 0 to 24 there. Also, I changed the var i inside the loop because that was confusing since you were using that to control the loop count and moved the vars outside the loop so there not redefined every pass.

Edit: See the first answer as well. I do think it was the var i being redefined inside the loop.

Reece
  • 123
  • 1
  • 9
  • 1
    This will give you an incorrect result. `value` is a **string**. And so if `f` is `"1"` and `j` is `"2"`, then `(f+j)` is `"12"`! If you then use `*` on it, it's turned into a number (`12`), and things go downhill from there. – T.J. Crowder Jun 07 '13 at 18:30
  • Ha, good catch. Just divide p and f by 1. Edit: wish I could see what their markup looks like. – Reece Jun 07 '13 at 18:42
  • @ Reece: There are about 50 says to make them numbers. One of the most popular was shown in the OP's question: `+` – T.J. Crowder Jun 07 '13 at 21:19