freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../basic-data-structures/check-for-the-presence-of-a...

4.0 KiB

id title challengeType videoUrl localeTitle
587d7b7b367417b2b2512b14 Check For The Presence of an Element With indexOf() 1 التحقق من وجود عنصر مع indexOf ()

Description

بما أن الصفائف يمكن تغييرها أو تحورها في أي وقت ، فلا يوجد ضمان حول مكان وجود بيانات معينة في صفيف معين ، أو حتى إذا كان هذا العنصر موجودًا حتى. لحسن الحظ ، توفر لنا جافا سكريبت طريقة أخرى مضمنة ، indexOf() ، تسمح لنا بالتحقق بسرعة وسهولة من وجود عنصر في صفيف. تأخذ indexOf() عنصرًا كمعلمة ، وعندما يتم استدعاؤها ، فإنها ترجع الموضع ، أو الفهرس ، لهذا العنصر ، أو -1 إذا كان العنصر غير موجود في الصفيف. فمثلا:
السماح للفواكه = [التفاح ، 'الكمثرى' ، 'البرتقال' ، 'الخوخ' ، 'الكمثرى'] ؛

fruits.indexOf ("التواريخ") // returns -1
fruit.indexOf ('oranges') // تُرجع 2
fruits.indexOf ('pears') // تُرجع 1 ، أول مؤشر موجود فيه العنصر

Instructions

يمكن أن يكون indexOf() مفيدا بشكل لا يصدق للتحقق بسرعة من وجود عنصر في صفيف. لقد حددنا وظيفة ، quickCheck ، والتي تأخذ مصفوفة وعنصر quickCheck . تعديل الدالة باستخدام indexOf() بحيث تقوم بإرجاع true إذا كان العنصر الذي تم تمريره موجودًا على الصفيف ، و false إذا لم يكن موجودًا.

Tests

tests:
  - text: '<code>quickCheck([&quot;squash&quot;, &quot;onions&quot;, &quot;shallots&quot;], &quot;mushrooms&quot;)</code> يجب أن تعود <code>false</code>'
    testString: 'assert.strictEqual(quickCheck(["squash", "onions", "shallots"], "mushrooms"), false, "<code>quickCheck(["squash", "onions", "shallots"], "mushrooms")</code> should return <code>false</code>");'
  - text: '<code>quickCheck([&quot;squash&quot;, &quot;onions&quot;, &quot;shallots&quot;], &quot;onions&quot;)</code> يجب أن تعود <code>true</code>'
    testString: 'assert.strictEqual(quickCheck(["squash", "onions", "shallots"], "onions"), true, "<code>quickCheck(["squash", "onions", "shallots"], "onions")</code> should return <code>true</code>");'
  - text: '<code>quickCheck([3, 5, 9, 125, 45, 2], 125)</code> يجب أن <code>quickCheck([3, 5, 9, 125, 45, 2], 125)</code> <code>true</code>'
    testString: 'assert.strictEqual(quickCheck([3, 5, 9, 125, 45, 2], 125), true, "<code>quickCheck([3, 5, 9, 125, 45, 2], 125)</code> should return <code>true</code>");'
  - text: '<code>quickCheck([true, false, false], undefined)</code> <code>false</code>'
    testString: 'assert.strictEqual(quickCheck([true, false, false], undefined), false, "<code>quickCheck([true, false, false], undefined)</code> should return <code>false</code>");'
  - text: يجب أن تستخدم الدالة <code>quickCheck</code> الأسلوب <code>indexOf()</code>
    testString: 'assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1, "The <code>quickCheck</code> function should utilize the <code>indexOf()</code> method");'

Challenge Seed

function quickCheck(arr, elem) {
  // change code below this line

  // change code above this line
}

// change code here to test different cases:
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));

Solution

// solution required