Files

50 lines
1.1 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Use the every Method to Check that Every Element in an Array Meets a Criteria
---
# Use the every Method to Check that Every Element in an Array Meets a Criteria
2018-10-12 15:37:13 -04: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.
#### 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)
---
## Hints
2018-10-12 15:37:13 -04:00
### Hint
Don't forget `return`.
---
## 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
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]);
```
</details>
2018-10-12 15:37:13 -04: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
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]);
```
</details>