2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Use the every Method to Check that Every Element in an Array Meets a Criteria
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Use the every Method to Check that Every Element in an Array Meets a Criteria
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Problem Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
Use the `every` method inside the `checkPositive` function to check if every element in `arr` is positive. The function should return a Boolean value.
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Relevant Links
|
2018-10-12 15:37:13 -04:00
|
|
|
- [Array.prototype.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
### Hint
|
|
|
|
Don't forget `return`.
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
|
|
|
function checkPositive(arr) {
|
|
|
|
// Add your code below this line
|
2019-07-24 00:59:27 -07:00
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
return arr.every(val => val > 0);
|
|
|
|
// Add your code above this line
|
|
|
|
}
|
|
|
|
checkPositive([1, 2, 3, -4, 5]);
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
function checkPositive(arr) {
|
|
|
|
// Add your code below this line
|
2019-07-24 00:59:27 -07:00
|
|
|
return arr.every(function(value) {
|
|
|
|
return value > 0;
|
|
|
|
});
|
2018-10-12 15:37:13 -04:00
|
|
|
// Add your code above this line
|
|
|
|
}
|
|
|
|
checkPositive([1, 2, 3, -4, 5]);
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|