JSFiddle - React, Tailwind, and code Playground

by kentaromiura

JavaScript

Array.prototype.contains = function contains(item) {

var length = this.length;
	if(length < 1) return false;
var from = Math.floor((arguments[1] || 0) -0);

	if(from === -Infinity) from = 0
	if(from < 0){
		from = length + from
	}
	if(from >= length || from > Math.pow(2, 32) -1) return false;

	var check = typeof item === 'number' && isNaN(item) ? isNaN : function(c){ return c === item };
  

	for(var i=from; i < length; i++)
		if(check(this[i])) return true;
	return false;

}

Array.prototype.contains = function (c) {
    return function contains(item) {
        console.log('<<', this, item, arguments[1], '>>')
        var result = c.apply(this, arguments);
        console.log('^ ', result, '\n');
        return result
    };
}(Array.prototype.contains);

$ERROR = alert;
Test262Error = Error;
    
if([1, 2, 3, 0, 0, 0].contains(0) !== true) $ERROR('should return true if the array contains the specified item');
if([0, 1, 2].contains('not found') !== false) $ERROR('should return false if the array does not contain the specified item'); 

/*
it('should return true if the array contains the specified item', function () {
    expect([1, 2, 3, 0, 0, 0].contains(0)).toBeTruthy();
});

it('should return false if the array does not contain the specified item', function () {
    expect([0, 1, 2].contains('not found')).toBeFalsy();
});
*/