Javascript Array isEmpty()

Description

Javascript Array isEmpty()


Array.prototype.isEmpty = function() { 
  return this.length === 0;
};

Javascript Array isEmpty()

?'use strict';/*  ww w. j a v a 2  s.com*/

Array.prototype.isEmpty = function () {
    return this && this.length === 0;
};

Javascript Array isEmpty()

Array.prototype.isEmpty = function () {
    return !(this.length > 0);
}

Javascript Array isEmpty()

Array.prototype.isEmpty = function() {
    return (0 === this.length);
}

Javascript Array isEmpty()

Array.prototype.isEmpty = function() { return this.length < 1 };

Javascript Array isEmpty()

function isEmpty (arr) {
  return arr.length == 0;
}

function isEmpty2 () {
  return this.length == 0;
}

Array.prototype.isEmpty = Array.prototype.isEmpty || isEmpty2;

var nums = [1, 2, 3, 4];
var other = [];/* ww w  . ja va 2s . c o  m*/

if (nums.length > 0) {
  console.log('nao vazio')
}

if (nums.length) {
  console.log('nao vazio, sem o operador ">"')
}

console.log(isEmpty(nums));

console.log(other.isEmpty())

console.log(nums.isEmpty());

console.log(isEmpty2.apply(nums));

Javascript Array isEmpty()

'use strict';/*from  w ww  .  ja  v a2 s. c o m*/

// Implement a queue using two stacks

let stack1 = [];
let stack2 = [];

Array.prototype.isEmpty = function() {
  if (this.length === 0) return true;
  else return false;
};

Array.prototype.peek = function() {
  return this[0];
};

function enqueue(num) {
  stack1.push(num);
}

function dequeue() {

  if (stack2.isEmpty()) {
    while (!stack1.isEmpty()) {
      stack2.push(stack1.pop());
    }
  }
  return stack2.shift();
}


enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);

console.log(dequeue());
console.log(dequeue());
console.log(dequeue());
console.log(dequeue());
console.log(dequeue());

Javascript Array isEmpty()

Array.prototype.isEmpty = function() {
    var me = this;
    return (0 == me.length);
};



PreviousNext

Related