@@ -14,7 +14,7 @@ colors.forEach(function(color) {
1414
1515var numbers = [ 1 , 2 , 3 , 4 , 5 ] ;
1616
17- //Create a variabl to hold the sum
17+ //Create a variable to hold the sum
1818
1919var sum = 0 ;
2020
@@ -76,7 +76,7 @@ var products = [
7676 { name : 'banana' , type : 'fruit' , quantity : 3 , price : 5 }
7777] ;
7878
79- var filteredProducts = [ ]
79+ var filteredProducts = [ ] ;
8080
8181for ( var i = 0 ; i < products . length ; i ++ ) {
8282 if ( products [ i ] . type === 'fruit' ) {
@@ -114,3 +114,191 @@ function commentsForPost(post, comments) {
114114
115115console . log ( commentsForPost ( post , comments ) ) ;
116116
117+ /** find **/
118+
119+ var users = [
120+ { name : 'jill' } ,
121+ { name : 'alex' } ,
122+ { name : 'bill' }
123+ ] ;
124+
125+ var user ;
126+
127+ for ( var i = 0 ; i < users . length ; i ++ ) {
128+ if ( users [ i ] . name === 'alex' ) {
129+ user = users [ i ] ;
130+ break ;
131+ }
132+ }
133+
134+ console . log ( user ) ;
135+
136+ var userFound = users . find ( function ( user ) { //will return only the first time it is found
137+ return user . name === "alex" ;
138+ } ) ;
139+
140+ console . log ( userFound ) ;
141+
142+ function Car ( model ) {
143+ this . model = model ;
144+ }
145+
146+ var cars = [
147+ new Car ( 'Buick' ) ,
148+ new Car ( 'Camaro' ) ,
149+ new Car ( 'Focus' )
150+ ] ;
151+
152+ var carFound = cars . find ( function ( car ) {
153+ return car . model === 'Focus' ;
154+ } ) ;
155+ console . log ( carFound ) ;
156+
157+ var posts = [
158+ { id : 1 , title : "new post" } ,
159+ { id : 2 , title : 'old post' }
160+ ] ;
161+
162+ var comment = { postId : 1 , content : 'Great Post' } ;
163+
164+ function postForComment ( posts , comment ) {
165+ return posts . find ( function ( post ) {
166+ return post . id === comment . postId ;
167+ } )
168+ }
169+
170+ console . log ( postForComment ( posts , comment ) ) ;
171+
172+ //every and some
173+ //with every we can view it as an && operator between each item in an array and
174+ //with some we can view it as an || operator between each item
175+
176+
177+ var computers = [
178+ { name : 'Apple' , ram : 24 } ,
179+ { name : 'Compaq' , ram : 4 } ,
180+ { name : 'Acer' , ram : 32 }
181+ ] ;
182+
183+ var allComputerCanRunProgram = true ;
184+
185+ var onlySomeCanRunProgram = false ;
186+
187+ for ( var i = 0 ; i < computers . length ; i ++ ) {
188+ var computer = computers [ i ] ;
189+
190+ if ( computer . ram < 16 ) {
191+ allComputerCanRunProgram = false ;
192+ } else {
193+ onlySomeCanRunProgram = true ;
194+ }
195+ }
196+
197+ console . log ( 'all: ' , allComputerCanRunProgram ) ;
198+ console . log ( 'some: ' , onlySomeCanRunProgram ) ;
199+
200+ var arrEvery = computers . every ( function ( computer ) {
201+ return computer . ram > 16 ;
202+ } ) ;
203+
204+ var arrSome = computers . some ( function ( computer ) {
205+ return computer . ram > 16 ;
206+ } ) ;
207+
208+ console . log ( 'every: ' , arrEvery ) ;
209+ console . log ( 'some: ' , arrSome ) ;
210+
211+ var names = [
212+ 'Alexandria' ,
213+ 'Matthew' ,
214+ 'Joe'
215+ ] ;
216+
217+ var namesEvery = names . every ( function ( name ) {
218+ return name . length > 4 ;
219+ } ) ;
220+
221+ var namesSome = names . some ( function ( name ) {
222+ return name . length > 4 ;
223+ } ) ;
224+
225+ console . log ( 'every: ' , namesEvery ) ;
226+ console . log ( 'some: ' , namesSome ) ;
227+
228+ function Field ( value ) {
229+ this . value = value ;
230+ }
231+
232+ Field . prototype . validate = function ( ) {
233+ return this . value . length > 0 ;
234+ }
235+
236+ var username = new Field ( '2cool' ) ;
237+ var password = new Field ( 'my_password' ) ;
238+ var birthdate = new Field ( '10/10/2010' ) ;
239+
240+
241+ var isValid = username . validate ( ) && password . validate ( ) && birthdate . validate ( ) ;
242+
243+ console . log ( 'validate: ' , isValid ) ;
244+
245+ var fields = [ username , password , birthdate ] ;
246+
247+ //this keeps it cleaner and more simple than declaring the variable above isValid and doing && statements for everything
248+ var formIsValid = fields . every ( function ( field ) {
249+ return field . validate ( ) ;
250+ } ) ;
251+
252+ if ( formIsValid ) {
253+ //allow user to submit!
254+ } else {
255+ //show an error msg
256+ }
257+
258+ //Reduce
259+
260+ var numbersArr = [ 10 , 20 , 30 ] ;
261+
262+ var sumArr = 0 ;
263+
264+ for ( var i = 0 ; i < numbersArr . length ; i ++ ) {
265+ sumArr += numbersArr [ i ] ;
266+ }
267+ console . log ( sumArr ) ;
268+
269+
270+ // here 0 is being passed in as the initial value, so 0 could be any starting value you want. It is coming into the
271+ //function as what "sum" is here. number is the individual number in each step through the numbersArr
272+ var reduceArr = numbersArr . reduce ( function ( sum , number ) {
273+ return sum + number ;
274+ } , 0 ) ;
275+
276+ console . log ( 'reduced array: ' , reduceArr ) ;
277+
278+ var primaryColors = [
279+ { color : 'red' } ,
280+ { color : 'yellow' } ,
281+ { color : 'blue' }
282+ ] ;
283+
284+ var justColors = primaryColors . reduce ( function ( previous , primaryColor ) {
285+ previous . push ( primaryColor . color ) ;
286+ return previous ;
287+ } , [ ] ) ;
288+
289+ console . log ( 'colors: ' , justColors ) ;
290+
291+
292+ //without the ! we would just get a count of numbers by adding in the ! we are saying we want a boolean and
293+ //0 = true every other number else is = false so this will work for our case. the final return statement here
294+ // will still returnthe counter if anything other than a parenthesis is found
295+ function balancedParens ( string ) {
296+ return ! string . split ( '' ) . reduce ( function ( counter , char ) {
297+ if ( counter < 0 ) { return counter } //this catches the case of )( where they still balance but not properly
298+ if ( char === '(' ) { return ++ counter } //adds a 1 if open
299+ if ( char === ')' ) { return -- counter } //decreases by 1 if closing
300+ return counter ; //if anything else is encountered we return the counter and ignore it
301+ } , 0 ) ;
302+ }
303+
304+ balancedParens ( '((((' ) ;
0 commit comments