Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions Array/every.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
*/
if (!Array.prototype.every) {
Array.prototype.every = function(callback, thisArg) {
Array.prototype.every = function(callbackfn, thisArg) {
'use strict';
var T, k;

if (this === void 0 || this === null) {
throw new TypeError('Array.prototype.every called on null or undefined');
if (this == null) {
throw new TypeError('this is null or not defined');
}

// 1. Let O be the result of calling ToObject passing the this
Expand All @@ -18,13 +16,15 @@ if (!Array.prototype.every) {
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;

// 4. If IsCallable(callback) is false, throw a TypeError exception.
if (callback.__class__ !== 'Function') {
throw new TypeError(callback + ' is not a function');
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (typeof callbackfn !== 'function' && Object.prototype.toString.call(callbackfn) !== '[object Function]') {
throw new TypeError();
}

// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
T = (arguments.length > 1) ? thisArg : void 0;
if (arguments.length > 1) {
T = thisArg;
}

// 6. Let k be 0.
k = 0;
Expand All @@ -41,15 +41,17 @@ if (!Array.prototype.every) {
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {

var testResult;
// i. Let kValue be the result of calling the Get internal method
// of O with argument Pk.
kValue = O[k];

// ii. Let testResult be the result of calling the Call internal method
// of callback with T as the this value and argument list
// containing kValue, k, and O.
var testResult = callback.call(T, kValue, k, O);
// ii. Let testResult be the result of calling the Call internal method
// of callbackfn with T as the this value if T is not undefined
// else is the result of calling callbackfn
// and argument list containing kValue, k, and O.
if(T) testResult = callbackfn.call(T, kValue, k, O);
else testResult = callbackfn(kValue,k,O)

// iii. If ToBoolean(testResult) is false, return false.
if (!testResult) {
Expand All @@ -60,4 +62,4 @@ if (!Array.prototype.every) {
}
return true;
};
}
}