Sobes.tech
Middle

/* Standart to'plam metodlarını genişleten intersection metodunu həyata keçirmək lazımdır. Bu metod başqa bir to'plam qəbul edir və yeni to'plam qaytarır, bu, ilkin to'plam və ötürülən to'plamın kəsişməsidir, yəni, yalnız hər iki to'plamda mövcud olan elementləri ehtiva edir. */ // kodunuz const set1 = new Set([1, 2, 3, 4]); const set2 = new Set([3, 4, 5, 6, 7, 8]); console.log(...set1.intersection(set2)) // 3 4 --- Set.prototype.intersection = function (set2) { const result = new Set(); for(const value of this) { if (set2.has(value)) result.add(value); } return result; } const set1 = new Set([1, 2, 3, 4]); const set2 = new Set([3, 4, 5, 6, 7, 8]); console.log(...set1.intersection(set2)) // 3 4

sobes.tech Süni İntellekt

AI-dan cavab

Set sinfi üçün intersection metodu, onu Set prototipinə əlavə etməklə həyata keçirilə bilər. Bu metod başqa bir to'plam qəbul edir və yalnız hər iki to'plamda olan elementləri ehtiva edən yeni to'plam qaytarır.

Set.prototype.intersection = function (otherSet) {
    const result = new Set();
    for (const item of this) {
        if (otherSet.has(item)) {
            result.add(item);
        }
    }
    return result;
};

const set1 = new Set([1, 2, 3, 4]);
const set2 = new Set([3, 4, 5, 6, 7, 8]);

console.log(...set1.intersection(set2)); // 3 4

Bu metod ilk to'plam elementlərini dövr edir və yalnız ikinci to'plamada olanları nəticəyə əlavə edir.