1

The JavaScript code below consists to print an alphanumeric array into two arrays, one letter, one number.

I used a "complex" system of prototype function and objects.

var utils = {
            sdNum: function(a, b) {
                return b-a;
            },
            saNum: function(a, b) {
                return a-b;
            }
        };

        var Separator = function() {
            this.aNum = Array();
            this.aChar = [];    
        };

        Separator.prototype.getNum = function() {
            for(var i = 0; i < this.alphanumeric.length; i++ )
                if(this.alphanumeric[i] === parseInt(this.alphanumeric[i]))
                    this.aNum.push(this.alphanumeric[i]);
            return this.aNum;

        };
        Separator.prototype.getChar = function() {
            for(var i = 0; i < this.alphanumeric.length; i++ )
                if(this.alphanumeric[i] !== parseInt(this.alphanumeric[i]))
                    this.aChar.push(this.alphanumeric[i]);
            return this.aChar;
        };

        Separator.prototype.sortNum = function(sorting = 'asc', va = this.getNum()) {
            if(sorting === 'asc')
                return va.sort(utils.saNum);

            return va.sort(utils.sdNum);
        };  
        Separator.prototype.sortChar = function(sorting, va = this.getChar()) {
            if(sorting === 'asc')
                return va.sort();

            return va.reverse();
        };


        var PrintSeparator = function(alphanumeric) {
            this.alphanumeric = alphanumeric;
        };

        PrintSeparator.prototype = new Separator();

        PrintSeparator.prototype.printChar = function(sorting = 'asc') {
            var tmp_str = '';

            tmp_str += '<h2><em>' + sorting + '</em> letter sorting</h2>',
            tmp_str += '<p>' + this.sortChar(sorting).join(' - ') + '</p>';

            return tmp_str;
        }
        PrintSeparator.prototype.printNum = function(sorting = 'asc') {
            var tmp_str = '';

            tmp_str += '<h2><em>' + sorting + '</em> number sorting</h2>',
            tmp_str += '<p>' + this.sortNum(sorting).join(' - ') + '</p>';

            return tmp_str;
        }

When i call a function for retrieve a result, all works fine.

var mixedArray1 = new PrintSeparator([11, 'a', 'g', 3, 8, 'c', 'b', 12, 9, 'd', 'f', 10, 'i', 2, 1, 7, 5, 'e', 'h', 4, 6]);

document.write(mixedArray1.printChar('desc'));

When i include the main script above into a js file, NetBeans go in error on this function:

Separator.prototype.sortNum = function(sorting = 'asc', va = this.getNum()) {

and the error is Expeted , but found =

Instead, if i embody the same script into the html page there is no error.

If i call the js page <script src="myscript.js"></script> there is always the NetBeans error but it works fine, instead if i call the same page <script src="mysqcript.js"/> id doesn't work.

Is there someone who may explain why please?

Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39
  • There are no default parameters in javascript. NetBeans is right, this declaration is simply invalid. No idea how this would work. – Bergi Jan 10 '14 at 14:39
  • @Bergi The crazy thing is it does, but its definitely not a recommended approach. See:http://jsfiddle.net/e4c7F/ – Kevin Bowersox Jan 10 '14 at 14:41
  • Also notice that you're doing [inheritance wrong](http://stackoverflow.com/q/10898786/1048572), all your `PrintSeparators` will [share their `aNum` and `aChar` array](http://stackoverflow.com/questions/10131052/crockfords-prototypal-inheritance-issues-with-nested-objects). – Bergi Jan 10 '14 at 14:45

1 Answers1

2

Expressions should not be placed within the declaration of a function. If you must assign default values for an argument do it from within the function.

        Separator.prototype.sortNum = function(sorting) {
            sorting = (sorting) ? sorting: 'asc';
            var va  = this.getNum();
            if(sorting === 'asc')
                return va.sort(utils.saNum);

            return va.sort(utils.sdNum);
        };  

The same goes for:

    Separator.prototype.sortChar = function(sorting) {
        var va = this.getChar();
        if(sorting === 'asc')
            return va.sort();

        return va.reverse();
    };

Regrading the script tags:

Always place a closing script tag with your scripts. When you use: <script src="mysqcript.js"/> the script isn't even loaded.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189