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

3.1 KiB

id title challengeType forumTopicId
587d7b7b367417b2b2512b14 Check For The Presence of an Element With indexOf() 1 301154

Description

Since arrays can be changed, or mutated, at any time, there's no guarantee about where a particular piece of data will be on a given array, or if that element even still exists. Luckily, JavaScript provides us with another built-in method, indexOf(), that allows us to quickly and easily check for the presence of an element on an array. indexOf() takes an element as a parameter, and when called, it returns the position, or index, of that element, or -1 if the element does not exist on the array. For example:
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];

fruits.indexOf('dates'); // returns -1
fruits.indexOf('oranges'); // returns 2
fruits.indexOf('pears'); // returns 1, the first index at which the element exists

Instructions

indexOf() can be incredibly useful for quickly checking for the presence of an element on an array. We have defined a function, quickCheck, that takes an array and an element as arguments. Modify the function using indexOf() so that it returns true if the passed element exists on the array, and false if it does not.

Tests

tests:
  - text: The <code>quickCheck</code> function should return a boolean (<code>true</code> or <code>false</code>), not a string (<code>"true"</code> or <code>"false"</code>)
    testString: assert.isBoolean(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
  - text: <code>quickCheck(["squash", "onions", "shallots"], "mushrooms")</code> should return <code>false</code>
    testString: assert.strictEqual(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'), false);
  - text: <code>quickCheck(["onions", "squash", "shallots"], "onions")</code> should return <code>true</code>
    testString: assert.strictEqual(quickCheck(['onions', 'squash', 'shallots'], 'onions'), true);
  - text: <code>quickCheck([3, 5, 9, 125, 45, 2], 125)</code> should return <code>true</code>
    testString: assert.strictEqual(quickCheck([3, 5, 9, 125, 45, 2], 125), true);
  - text: <code>quickCheck([true, false, false], undefined)</code> should return <code>false</code>
    testString: assert.strictEqual(quickCheck([true, false, false], undefined), false);
  - text: The <code>quickCheck</code> function should utilize the <code>indexOf()</code> method
    testString: assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1);

Challenge Seed

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

  // Only change code above this line
}

console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));

Solution

function quickCheck(arr, elem) {
  return arr.indexOf(elem) >= 0; 
}