149 lines
7.2 KiB
Markdown
149 lines
7.2 KiB
Markdown
![]() |
---
|
|||
|
title: Diff Two Arrays
|
|||
|
localeTitle: الفرق صفيفتين
|
|||
|
---
|
|||
|

|
|||
|
|
|||
|
 تذكر استخدام **`Read-Search-Ask`** إذا واجهتك مشكلة. حاول إقران البرنامج  واكتب الكود الخاص بك 
|
|||
|
|
|||
|
###  شرح المشكلة:
|
|||
|
|
|||
|
تحقق من صفيفين ثم قم بإرجاع صفيف جديد يحتوي فقط على العناصر غير الموجودة في أي من الصفائف الأصلية.
|
|||
|
|
|||
|
#### روابط ذات صلة
|
|||
|
|
|||
|
* [للحلقة (Devdocs)](https://devdocs.io/javascript/statements/for)
|
|||
|
* [Array.prototype.includes (Devdocs)](https://devdocs.io/javascript/global_objects/array/includes)
|
|||
|
* [Array.prototype.filter (Devdocs)](https://devdocs.io/javascript/global_objects/array/filter)
|
|||
|
* [Array.prototype.concat (Devdocs)](https://devdocs.io/javascript/global_objects/array/concat)
|
|||
|
|
|||
|
##  تلميح: 1
|
|||
|
|
|||
|
دمج القائمة لتسهيل مقارنة الوظائف.
|
|||
|
|
|||
|
> _حاول أن تحل المشكلة الآن_
|
|||
|
|
|||
|
##  تلميح: 2
|
|||
|
|
|||
|
استخدم الفلتر للحصول على المصفوفة الجديدة ، ستحتاج إلى إنشاء وظيفة رد اتصال.
|
|||
|
|
|||
|
> _حاول أن تحل المشكلة الآن_
|
|||
|
|
|||
|
##  تلميح: 3
|
|||
|
|
|||
|
أفضل طريقة للانتقال إلى وظيفة رد الاتصال هي التحقق مما إذا كان الرقم من الصفيف المضمن الجديد ليس في **كلا** المصفوفين الأصليين وإعادته.
|
|||
|
|
|||
|
> _حاول أن تحل المشكلة الآن_
|
|||
|
|
|||
|
## تنبيه المفسد!
|
|||
|
|
|||
|

|
|||
|
|
|||
|
**الحل في المستقبل!**
|
|||
|
|
|||
|
##  الحل الأساسي للكود (الحل الأمني):
|
|||
|
|
|||
|
` function diffArray(arr1, arr2) {
|
|||
|
var newArr = [];
|
|||
|
|
|||
|
function onlyInFirst(first, second) {
|
|||
|
// Looping through an array to find elements that don't exist in another array
|
|||
|
for (var i=0;i<first.length;i++) {
|
|||
|
if (second.indexOf(first[i]) === -1) {
|
|||
|
// Pushing the elements unique to first to newArr
|
|||
|
newArr.push(first[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
onlyInFirst(arr1, arr2);
|
|||
|
onlyInFirst(arr2, arr1);
|
|||
|
|
|||
|
return newArr;
|
|||
|
}
|
|||
|
|
|||
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
|||
|
`
|
|||
|
|
|||
|
 [تشغيل الكود](https://repl.it/CLme/0)
|
|||
|
|
|||
|
### شرح الشفرة:
|
|||
|
|
|||
|
قراءة التعليقات في الكود.
|
|||
|
|
|||
|
#### روابط ذات صلة
|
|||
|
|
|||
|
* [للحلقة (Devdocs)](https://devdocs.io/javascript/statements/for)
|
|||
|
|
|||
|
##  حل الشفرة الوسيطة (الحل التعريفي):
|
|||
|
|
|||
|
` function diffArray(arr1, arr2) {
|
|||
|
return arr1
|
|||
|
.concat(arr2)
|
|||
|
.filter(
|
|||
|
item => !arr1.includes(item) || !arr2.includes(item)
|
|||
|
)
|
|||
|
}
|
|||
|
|
|||
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
|||
|
`
|
|||
|
|
|||
|
 [تشغيل الكود](https://repl.it/CNYb/0)
|
|||
|
|
|||
|
### شرح الشفرة:
|
|||
|
|
|||
|
اشرح الحل هنا وأضف أي روابط ذات صلة
|
|||
|
|
|||
|
#### روابط ذات صلة
|
|||
|
|
|||
|
* [Array.prototype.concat (Devdocs)](https://devdocs.io/javascript/global_objects/array/concat)
|
|||
|
* [Array.prototype.filter (Devdocs)](https://devdocs.io/javascript/global_objects/array/filter)
|
|||
|
* [Array.prototype.includes (Devdocs)](https://devdocs.io/javascript/global_objects/array/includes)
|
|||
|
|
|||
|
##  الحل المتقدم للكود (الحل التعريفي):
|
|||
|
|
|||
|
`function diffArray(arr1, arr2) {
|
|||
|
return arr1
|
|||
|
.filter(el => !arr2.includes(el))
|
|||
|
.concat(
|
|||
|
arr2.filter(el => !arr1.includes(el))
|
|||
|
)
|
|||
|
}
|
|||
|
|
|||
|
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
|||
|
`
|
|||
|
|
|||
|
 [تشغيل الكود](https://repl.it/CNYU/0)
|
|||
|
|
|||
|
### شرح الشفرة:
|
|||
|
|
|||
|
اشرح الحل هنا وأضف أي روابط ذات صلة
|
|||
|
|
|||
|
##  البديل المتقدم للحلول البرمجية (الحل التعريفي):
|
|||
|
|
|||
|
`function diffArray(arr1, arr2) {
|
|||
|
return [
|
|||
|
...diff(arr1, arr2),
|
|||
|
...diff(arr2, arr1)
|
|||
|
]
|
|||
|
|
|||
|
function diff(a, b) {
|
|||
|
return a.filter(item => b.indexOf(item) === -1);
|
|||
|
}
|
|||
|
}
|
|||
|
`
|
|||
|
|
|||
|
#### روابط ذات صلة
|
|||
|
|
|||
|
* [Array.prototype.includes (Devdocs)](https://devdocs.io/javascript/global_objects/array/includes)
|
|||
|
* [Array.prototype.filter (Devdocs)](https://devdocs.io/javascript/global_objects/array/filter)
|
|||
|
* [Array.prototype.concat (Devdocs)](https://devdocs.io/javascript/global_objects/array/concat)
|
|||
|
|
|||
|
##  ملاحظات للمساهمات:
|
|||
|
|
|||
|
*  **لا تقم** بإضافة حلول مشابهة لأي حلول موجودة. إذا كنت تعتقد أنها **_مشابهة ولكن أفضل_** ، فحاول دمج (أو استبدال) الحل المشابه الموجود.
|
|||
|
* أضف شرحًا لحلك.
|
|||
|
* تصنيف الحل في واحدة من الفئات التالية - **الأساسي** **والمتوسط** **والمتقدم** . 
|
|||
|
* الرجاء إضافة اسم المستخدم الخاص بك فقط إذا قمت بإضافة أي **محتويات رئيسية ذات صلة** . (  **_لا_** _تزيل أي أسماء مستخدمين حالية_ )
|
|||
|
|
|||
|
> نرى  [**`Wiki Challenge Solution Template`**](http://forum.freecodecamp.com/t/algorithm-article-template/14272) كمرجع.
|