Git Product home page Git Product logo

fewpjs-iterators-find-lab-sgharms-test-webdev-fund's Introduction

Iterator Drill: Find

Learning Goals

  • Demonstrate find()
  • Use find() to isolate a specific result

Introduction

In any programming language, you'll perform operations on arrays. Looking in an Array for a sub string or set of characters is a common task. Sometimes you need to look for a specific item in the array, and return it. Given a string or array you can iterate over its elements and perform actions. ES6 introduced a few new Array methods--one of them being find().

Demonstrate find()

The find function behaves similar to others in JavaScript (like filter) that want a truthy/falsey result. find returns the first element for which the function returns true, a single element.

[1,3,5,6,8].find( e => e % 2 === 0 )

Note: You could have multiple matches, but find returns only the first element. This could lead to some bugs if you're not careful.

let roommates = ["jess", "winston", "winston", "nick"];

roommates.find( s => s === "winston" )
// => winston

roommates.filter( s => "winston" )
//=> [winston, winston]

Use 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 football team Kansas City Chiefs. 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:

const record = [
  {year: "2018", result: "L"},
  {year: "2017", result: "W"},
  {year: "2016", result: "N/A"}
  //...
]

Write a function called superbowlWin() in index.js that:

  • Receives 1 argument, an Array of JavaScript Objects
  • Each object has two properties: year and result
  • Use find() to test each Object to see if the result is "W" โ€” a win!
  • superbowlWin() should return the year when the win occurred (if it occurred at all!). If no win is found, it should return, sadly, undefined

Run the tests using learn.

Conclusion

Array.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 returns the first match.

Resources

fewpjs-iterators-find-lab-sgharms-test-webdev-fund's People

Contributors

drakeltheryuujin avatar maxwellbenton avatar rrcobb avatar dependabot[bot] avatar

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar  avatar Joe Cardarelli avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Scott Ungchusri avatar Nicole Kroese  avatar Lore Dirick avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.