.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
add-elements-to-the-end-of-an-array-using-concat-instead-of-push.md
apply-functional-programming-to-convert-strings-to-url-slugs.md
avoid-mutations-and-side-effects-using-functional-programming.md
combine-an-array-into-a-string-using-the-join-method.md
combine-two-arrays-using-the-concat-method.md
implement-map-on-a-prototype.md
implement-the-filter-method-on-a-prototype.md
introduction-to-currying-and-partial-application.md
learn-about-functional-programming.md
pass-arguments-to-avoid-external-dependence-in-a-function.md
refactor-global-variables-out-of-functions.md
remove-elements-from-an-array-using-slice-instead-of-splice.md
return-a-sorted-array-without-changing-the-original-array.md
return-part-of-an-array-using-the-slice-method.md
sort-an-array-alphabetically-using-the-sort-method.md
split-a-string-into-an-array-using-the-split-method.md
understand-functional-programming-terminology.md
understand-the-hazards-of-using-imperative-code.md
use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.md
use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.md
use-the-filter-method-to-extract-data-from-an-array.md
use-the-map-method-to-extract-data-from-an-array.md
use-the-reduce-method-to-analyze-data.md
use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.md
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
.markdownlint.yaml
dictionaries
schema
test
.babelrc
LICENSE.md
create-challenge-bundle.js
crowdin.yml
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc-base.json
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
77 lines
1.7 KiB
Markdown
77 lines
1.7 KiB
Markdown
---
|
|
id: 587d7dab367417b2b2512b6e
|
|
title: Use the every Method to Check that Every Element in an Array Meets a Criteria
|
|
challengeType: 1
|
|
forumTopicId: 301312
|
|
dashedName: use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria
|
|
---
|
|
|
|
# --description--
|
|
|
|
The `every` method works with arrays to check if *every* element passes a particular test. It returns a Boolean value - `true` if all values meet the criteria, `false` if not.
|
|
|
|
For example, the following code would check if every element in the `numbers` array is less than 10:
|
|
|
|
```js
|
|
var numbers = [1, 5, 8, 0, 10, 11];
|
|
numbers.every(function(currentValue) {
|
|
return currentValue < 10;
|
|
});
|
|
// Returns false
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Use the `every` method inside the `checkPositive` function to check if every element in `arr` is positive. The function should return a Boolean value.
|
|
|
|
# --hints--
|
|
|
|
Your code should use the `every` method.
|
|
|
|
```js
|
|
assert(code.match(/\.every/g));
|
|
```
|
|
|
|
`checkPositive([1, 2, 3, -4, 5])` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(checkPositive([1, 2, 3, -4, 5]));
|
|
```
|
|
|
|
`checkPositive([1, 2, 3, 4, 5])` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(checkPositive([1, 2, 3, 4, 5]));
|
|
```
|
|
|
|
`checkPositive([1, -2, 3, -4, 5])` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(checkPositive([1, -2, 3, -4, 5]));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function checkPositive(arr) {
|
|
// Only change code below this line
|
|
|
|
|
|
// Only change code above this line
|
|
}
|
|
checkPositive([1, 2, 3, -4, 5]);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function checkPositive(arr) {
|
|
// Only change code below this line
|
|
return arr.every(num => num > 0);
|
|
// Only change code above this line
|
|
}
|
|
checkPositive([1, 2, 3, -4, 5]);
|
|
```
|