freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-an.../basic-algorithm-scripting/finders-keepers.md

1.2 KiB

id title challengeType forumTopicId dashedName
a6e40f1041b06c996f7b2406 Chi trova tiene 1 16016 finders-keepers

--description--

Crea una funzione che scorre un array arr e restituisce il primo elemento che passa un 'test di verità'. Ciò significa che, dato un elemento x, il 'test di verità' viene superato se func(x) è true. Se nessun elemento supera il test, restituisce undefined.

--hints--

findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }) dovrebbe restituire 8.

assert.strictEqual(
  findElement([1, 3, 5, 8, 9, 10], function (num) {
    return num % 2 === 0;
  }),
  8
);

findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; }) dovrebbe restituire undefined.

assert.strictEqual(
  findElement([1, 3, 5, 9], function (num) {
    return num % 2 === 0;
  }),
  undefined
);

--seed--

--seed-contents--

function findElement(arr, func) {
  let num = 0;
  return num;
}

findElement([1, 2, 3, 4], num => num % 2 === 0);

--solutions--

function findElement(arr, func) {
  return arr.filter(func)[0];
}

findElement([1, 2, 3, 4], num => num % 2 === 0);