Files
2022-01-20 20:30:18 +01:00

1.3 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
a6e40f1041b06c996f7b2406 条件を満たす要素の抽出 5 16016 finders-keepers

--description--

配列 arr を参照し、「真偽判定」で真となる最初の要素を返す関数を作成してください。 たとえば、要素 x が与えられた場合、func(x)true であれば、「真偽判定」は真となります。 真となる要素が無い場合は、undefined を返してください。

--hints--

findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; })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; })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);