freeCodeCamp/curriculum/challenges/russian/02-javascript-algorithms-an.../basic-algorithm-scripting/falsy-bouncer.russian.md

2.0 KiB
Raw Blame History

id title isRequired challengeType forumTopicId localeTitle
adf08ec01beb4f99fc7a68f2 Falsy Bouncer true 5 16014 Фальшивый вышибала

Description

Удалите все значения фальши из массива. Значения фальши в JavaScript - false , null , 0 , "" , undefined и NaN . Подсказка: попробуйте преобразовать каждое значение в логическое. Не забудьте использовать Read-Search-Ask, если вы застряли. Напишите свой собственный код.

Instructions

Tests

tests:
  - text: <code>bouncer([7, "ate", "", false, 9])</code> should return <code>[7, "ate", 9]</code>.
    testString: assert.deepEqual(bouncer([7, "ate", "", false, 9]), [7, "ate", 9]);
  - text: <code>bouncer(["a", "b", "c"])</code> should return <code>["a", "b", "c"]</code>.
    testString: assert.deepEqual(bouncer(["a", "b", "c"]), ["a", "b", "c"]);
  - text: <code>bouncer([false, null, 0, NaN, undefined, ""])</code> should return <code>[]</code>.
    testString: assert.deepEqual(bouncer([false, null, 0, NaN, undefined, ""]), []);
  - text: <code>bouncer([1, null, NaN, 2, undefined])</code> should return <code>[1, 2]</code>.
    testString: assert.deepEqual(bouncer([1, null, NaN, 2, undefined]), [1, 2]);

Challenge Seed

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr;
}

bouncer([7, "ate", "", false, 9]);

Solution

function bouncer(arr) {
  return arr.filter(e => e);
}

bouncer([7, "ate", "", false, 9]);