Print array values ​​in js

I am trying to create a function that will be run through an array and get its value for a string that looks like this: '[1,2,3]'. I also need it to represent only part of the array in some cases, according to the given index. For example: an array [1,2,0], printed from index 0 to index 1, would look like this: '[1,2]'. For some reason, my function does not give any output at all. Here he is:

function Tower(size, isFull) { this.count = 0; this.tower = new Array(size); this.add = function(disk) { this.tower[this.count++] = disk; }; if (isFull) { for (var i = 1; i <= size; i++) { this.add(i); }; } this.canAdd = function(disk) { return this.count == 0 || this.tower[this.count - 1] > disk; }; this.getTopDiskValue = function() { return (this.count > 0) ? this.tower[this.count - 1] : 0; }; this.popTop = function() { return this.tower[--this.count]; }; this.isFull = function() { return this.count == this.tower.length; }; this.printable = function() { var output = "["; for (var i = 0; i < this.count; i++) { output += "" + this.tower[i] + ','; } return output.substring(0, output.length() - 1) + (output.length() > 1 ? ']' : ""); }; } 

I expect the printable () function to return a string so that the code:

 var tower = new Tower(3,true); alert(tower.printable()); 

a warning window appears with the text '[1,2,3]' on it. This object is a translation from Java. It worked perfectly in java btw, I think the translation is not perfect.

+4
source share
3 answers

JavaScript is not Java - you are not getting the length of the array or string by calling its .length() method, but just returning its .length property. An exception that is thrown when trying to call a number is that your script crashes and prevents a warning. This will work:

 this.printable = function() { var output = "["; for (var i = 0; i < this.count; i++) { output += "" + this.tower[i] + ','; } return output.substring(0, output.length - 1) + (output.length > 1 ? ']' : ""); }; 

However, you can use your own .join() method to combine the values ​​of the array. In addition, you should add your methods in the prototype of Tower objects:

 Tower.prototype.printable = function() { if (this.count) return "[" + this.tower.slice(0, this.count).join(",") + "]"; else return ""; }; 

Btw: Usually this method is called toString - not only for convenience, but it will also be used when the Tower object is called on a string value.

+1
source

What you do is too complicated.

Say you have an array declared as

 var array = [1, 2, 3]; 

you will get the desired line with

 return '['+array.join(',')+']'; 

You do not need pop or add functions, they are also native (and highly optimized):

 var last = array.pop() array.push(newItem); 

Link:

Please note that all browsers offer a console where you will find a detailed explanation of your mistakes. Pay attention, for example, to the Chrome Developer Tools .

+2
source

use the Array.join() method:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/join


 this.printable = function() { var output = new Array(); for (var i = 0; i < this.count; i++) { output.push(this.tower[i]); } return output.length == 0 ? "" : "[" + output.join(",") + ']'; }; 

or if it's as simple as it looks:

 this.printable = function() { return this.count == 0 ? "" : "[" + this.tower.join(",") + ']'; }; 
+1
source

Source: https://habr.com/ru/post/1437242/


All Articles