Array.prototype.getLevel
Sunny-117 opened this issue · comments
Array.prototype.getLevel = function () {
let max = 1
for (const a of this) {
if (a instanceof Array) {
const depth = a.getLevel() + 1
if (max < depth) max = depth
}
}
return max
};
const a1 = [1, 2, [1], [1, [2, [3]]]];
console.log(a1.getLevel()); //4
Array.prototype.getLevel = function(level, currentLevel) {
currentLevel = currentLevel || 0;
var count = 0;
for (var i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
count += this[i].getLevel(level, currentLevel + 1);
}
if (currentLevel === level - 1) {
count += 1;
}
}
return count;
};