mdn / sprints

Archived: MDN Web Docs issues are tracked in the content repository.
https://github.com/mdn/content
Creative Commons Zero v1.0 Universal
150 stars 142 forks source link

Updation in the body and code #3952

Closed shivamrathi99 closed 3 years ago

shivamrathi99 commented 3 years ago

URL(s)

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

Request type

Details

This below text is not clearly justifying the concept of "some" method in an array.

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value. const array = [1, 2, 3, 4, 5];

// checks whether an element is even const even = (element) => element % 2 === 0;

console.log(array.some(even)); // expected output: true

it would improve the readability and will be easier to understand if it would be :

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value. Some() does not mutate the array on which it is called.

const array = [1, 2, 3, 4, 5]; // checks whether an element is even const even = array.some(element => element % 2 === 0); console.log(even); // expected output: true

Thanks.

RNabla commented 3 years ago

Implementation of some does not modify, however provided callback could mutate array and tested objects. It's possible but it is asking for troubles. In my opinion using some should be idempotent - subsequent calls should have return the same test result.

"some does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn." - Taken from https://tc39.es/ecma262/#sec-array.prototype.some

Here's example:

!(function () {
    const array = [1, 2, 3, 4];
    const callback = function (obj, index, arr) {
        if (obj === 42) {
            return true;
        }
        else if (obj % 2 === 0) {
            arr.push(42);
        }
        return false;
    };
    // [1, 2, 3, 4]
    console.log('Start:', JSON.parse(JSON.stringify(array)));
    // false
    console.log('Some: ', array.some(callback));
    // true
    console.log('Some: ', array.some(callback));
    // [1, 2, 3, 4, 42, 42]
    console.log('End', JSON.parse(JSON.stringify(array)));
})();

Please don't embed that on MDN, it's a bad code. However I would update description to be in sync with ECMAscript.

shivamrathi99 commented 3 years ago

thanks for the explanation @RNabla

chrisdavidmills commented 3 years ago

Issue moved to mdn/content #1442 via ZenHub