Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mosnad-web01/abdulaziz-phase-1-array-find-method-lab-master
https://github.com/mosnad-web01/abdulaziz-phase-1-array-find-method-lab-master
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/mosnad-web01/abdulaziz-phase-1-array-find-method-lab-master
- Owner: Mosnad-Web01
- License: other
- Created: 2024-08-20T10:55:27.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-08-20T10:57:13.000Z (5 months ago)
- Last Synced: 2024-11-29T12:31:12.264Z (2 months ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Using the Array Find Method
## Learning Goals
* Practice using `find()` to isolate a specific result
## Introduction
We have learned that the `indexOf()` and `find()` methods can be used to locate
an element in an array, and that they return the **first** element for which the
condition is met. (If you instead want a list of _all_ elements that meet a
condition, you would use `Array.prototype.filter()` instead. We'll learn about
`filter()` in the next lesson.)We've also learned that the `find()` method takes a _callback function_ as
its argument. This is a common pattern for the `Array` methods we'll be
learning about in this section — in fact, `indexOf()` is the only one that
_doesn't_ take a callback function as an argument. `find()` is one of two
`Array` methods that expects the callback function to return either `true` or
`false` (`filter()` is the other one).In this lab, we'll practice using the `find()` method.
## Practice Using `find()` to Isolate a Specific Result
Let's create a function that uses the method `find()` to single out a specific
result in game record data for the Denver Broncos football team. We want to
find out if, at _any_ point in the team's existence, they've had a win in the
Superbowl. Our data looks like this:```js
const record = [
{ year: "2015", result: "W"},
{ year: "2014", result: "N/A"},
{ year: "2013", result: "L"},
//...
]
```Write a function called `superbowlWin()` in `index.js`:
* The function should receive 1 argument, an `Array` of JavaScript `Object`s
* Each object has two properties: `year` and `result`
* It should use `find()` to test each `Object` to see if the `result` is `"W"` —
a win!
* It should return the `year` when the win occurred (if it occurred at all!)
* If no win is found, it should return, sadly, `undefined`Remember the workflow:
1. Install the dependencies using `npm install`.
2. Run the tests using `npm test`.
3. Read the errors; vocalize what they're asking you to do.
4. Write code; repeat steps 2 and 3 often until a test passes.
5. Repeat as needed for the remaining tests.After you have all the tests passing, remember to commit and push your changes
up to GitHub, then submit your work to Canvas using CodeGrade.## Conclusion
`Array.prototype.find()` is a built-in function in JavaScript which is used to
get the value of the first element in the array that satisfies the provided
condition. With this, you can quickly check all the elements of the array and
return the first match.## Resources
* [MDN: Array.prototype.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)