Includes Polyfills for Arraty and String

PHOTO EMBED

Sun Oct 02 2022 04:02:45 GMT+0000 (Coordinated Universal Time)

Saved by @vaibhav_matere #javascript

function myIncludes(element) {
    const isThisArray = Array.isArray(this)
    if (isThisArray) {
        for (let i = 0; i < this.length; i++){
            if (this[i] === element) {
                return true;
            }
        }
    } else {
        for (let i = 0; i < this.length; i++){
            if (this[i] === element[0]) {
                for (let j = 1; j < element.length; j++){
                    if (this[i + j] !== element[j]) {
                        return false
                    }
                }
                return true;
            }
        }
    }
    return false; 
}
content_copyCOPY

Array.prototype.myIncludes = myIncludes; String.prototype.myIncludes = myIncludes; const array1 = [1, 2, 3]; console.log(array1.myIncludes(2)); // expected output: true const name = "Vaibhav"; console.log(name.myIncludes("Vai")) // expected output: true console.log(name.myIncludes("vai")) // expected output: false