freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../functional-programming/use-the-every-method-to-che...

1.6 KiB

id title challengeType forumTopicId dashedName
587d7dab367417b2b2512b6e Usar o método every para checar se todos os elementos em um array atendem a um critério 1 301312 use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria

--description--

O método every funciona verificando se todos os elementos de um array passam em um teste. Ele retorna um booleano: true se todos os valores atendem ao critério e false caso contrário.

Por exemplo, o código a seguir verifica se todos os elementos no array numbers são menores que 10:

const numbers = [1, 5, 8, 0, 10, 11];

numbers.every(function(currentValue) {
  return currentValue < 10;
});

O método every retorna false neste exemplo.

--instructions--

Use o método every dentro da função checkPositive para checar se todos os elementos em arr são positivos. A função deve retornar um valor booleano.

--hints--

Você deve usar o método every.

assert(code.match(/\.every/g));

checkPositive([1, 2, 3, -4, 5]) deve retornar false.

assert.isFalse(checkPositive([1, 2, 3, -4, 5]));

checkPositive([1, 2, 3, 4, 5]) deve retornar true.

assert.isTrue(checkPositive([1, 2, 3, 4, 5]));

checkPositive([1, -2, 3, -4, 5]) deve retornar false.

assert.isFalse(checkPositive([1, -2, 3, -4, 5]));

--seed--

--seed-contents--

function checkPositive(arr) {
  // Only change code below this line


  // Only change code above this line
}

checkPositive([1, 2, 3, -4, 5]);

--solutions--

function checkPositive(arr) {
  return arr.every(num => num > 0);
}