tags:

views:

83

answers:

4

I've been trying to debug this script all day and I got nothing any help would be greatly appreciated.

I keep getting an error that it's missing a curly brace or a square brace or a parenthetical. And also when I try to get the value of my options it says x is undefined but when I input 0 as my index I can retrieve opts.value, my question is why is x undefined and what's missing from this script

prices= (function(){    
          table=document.getElementsByTagName("table");
          selects=table[0].getElementsByTagName("select");
          for(var x=0;x<=10;x++){
            opts=new Array();
            opts=selects[x].getElementsByTagName("option")[selects[x].selectedIndex];
          }
          return{
            value: (function(){
                     val=new Array();
                     for(var i=0;i<=5;i++){
                       val[i]=opts.value
                     }
                     return val;
                   })();,
            total: (function(){}
                     var num="$15.00"
                     var t;
                     for(var j=0;j<=3;j++){
                       t+=num.slice(1).valueOf();
                     }
                     return t
                    })();
          };    
})();

var hello="hello"
document.write("<p>hello</p>");//line just test whether or not function is working
document.write(opts.value);
+1  A: 

Well, if that script is in the <head> of your document, its trying to read DOM elements before they've been created, unless you're calling it via onload which you're not showing.

Your other option would be to put the <script> ... </script> at the bottom of your page right before the closing body. While I can't be 100% sure this is the problem because you haven't posted your HTML, this is a fair guess if you're getting undefined DOM elements.

Erik
A: 

It seems that return t is missing a semi-colon.

Also, in the anon-function following total:, you seem to have paired-brackets {} followed by more body, which finally ends with a mismatched closed-bracket just before return t.

If you fix those issues, I think you will be much further along towards having working code

abelenky
when I fix my code I still get x is undefined
Daquan Hall
A: 

I didn't so much check what your code does but tried to find an answer to your missing braces / syntactical errors. It seems there are quite a few. I reformatted your above code, using proper indenting, and came up with this:

var prices = (function() {
    table = document.getElementsByTagName("table");
    selects = table[0].getElementsByTagName("select");
    for (var x = 0; x <= 10; x++) {
        opts = new Array();
        opts = selects[x].getElementsByTagName("option")[selects[x].selectedIndex];
    }
    return {
        value:
            (function() {
                var val = new Array();
                for (var i = 0; i <= 5; i++) {
                    val[i] = opts.value;
                }
                return val;
            })() ,
        total:
            (function() {
                var num = "$15.00";
                var t;
                for(var j = 0; j <= 3; j++) {
                    t += num.slice(1).valueOf();
                }
                return t;
            })()
    };
})();

Some things to note here:

  • (function() { ... })() defines a function (the function() { ... } bit) and then immediately calls it (the terminating () bit). The whole expression has the value of whatever the function returns.

  • return { value: ..., total: ... } returns a dictionary, which can be referenced by the prices variable. Note that you were missing the var keyword before prices = ... (and also before val. Also, after total: function(), you need to remove the closing curly brace, since what follows belongs to that function.

  • You assign twice to opts (for which the var keyword might also be missing, unless it's declared outside the function). Perhaps you should write var opts = new Array() outside the for loop and then assign to opts[x] inside the for loop?

stakx
I'm still receiving x is undefined
Daquan Hall
I tried to reproduce your error (using Firebug). While I don't get a `x is undefined` error, I get a `selects[x] is undefined` error if there are not as many `select` elements as your `for` loop expects. It might help if you post the fragment of your HTML which contains the `table` with the `option` and `select` elements.
stakx
+1  A: 

Closure Demo

Sorry if I missed anything, I didn't try running it because I've kept the html scenario you were using. Because it's using document.write, it will return the default selected option values from 11 selects in first table on the page.

// I've changed your "demo" to show the use of a "closure"...
var prices = (function() { //maybe good to add var
    var table = document.getElementsByTagName("table"); //maybe good to add var
    var selects = table[0].getElementsByTagName("select"); //maybe good to add var

    var opts = new Array(); //pull it out of for loop and add var! FYI var opts = []; is equivalent.
    for (var x = 0; x <= 10; x++) { //your test requires at least eleven selects in the first table.
        opts[x] = selects[x].options[selects[x].selectedIndex]; // maybe wanted 11 selected options in a closure?
    }
    return { // remove both anonymous wrappers from your object literal (function(){ ... })();
        value: function() {
            var val = new Array();  //maybe very good to add var
            for (var i = 0; i <= 5; i++) {
                val[i] = opts[i].value; //good to add ; read selected option.value x6 from opts array in closure!
            }
            return val;
        },
        total: function() { // deleted an extra }
            var num = "$15.00"; //add ;
            var t; // must initialize to 0 since you're using +=
            for (var j = 0; j <= 3; j++) {
                t += num.slice(1).valueOf(); //  null +=15.00 x3
            }
            return t; // null, good to add ;
        }
    };
})();
// I'm guessing you've done too much work to have previously defined opts as a global, yes?
document.write(prices.value().join('\r\n<br />')); // selected values copied from the closure

/* //using document.write so all this need to run at the bottom of a page
var hello = "hello"; //good to add ;
document.write("<p>hello</p>"); //line just test whether or not function is working
document.write(opts.value); 
*/

See the comments for help fixing up price.total etc.

machine elf
+1 for correcting and documenting the original code. Nice work!
stakx
Thanks alot for your help I finally got it working
Daquan Hall
machine elf