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?