|
23 | 23 | { text: 'Nice Nice Nice!', id: 542328 } |
24 | 24 | ]; |
25 | 25 |
|
| 26 | + const year = (new Date()).getFullYear() |
| 27 | + const age = (personYear) => year - personYear |
| 28 | + const nineteen = (person) => person.year >= 19 |
| 29 | + |
26 | 30 | // Some and Every Checks |
27 | 31 | // Array.prototype.some() // is at least one person 19? |
28 | | - // const isAdult = people.some(function(person) { |
29 | | - // const currentYear = (new Date()).getFullYear(); |
30 | | - // if(currentYear - person.year >= 19) { |
31 | | - // return true; |
32 | | - // } |
33 | | - // }); |
34 | | - |
35 | | - const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19); |
| 32 | + const isAdult = people.some(nineteen) |
| 33 | + console.log({isAdult}) |
36 | 34 |
|
37 | | - console.log({isAdult}); |
38 | 35 | // Array.prototype.every() // is everyone 19? |
39 | | - |
40 | | - const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19); |
41 | | - console.log({allAdults}); |
| 36 | + const allAdults = people.every(nineteen) |
| 37 | + console.log({allAdults}) |
42 | 38 |
|
43 | 39 | // Array.prototype.find() |
44 | 40 | // Find is like filter, but instead returns just the one you are looking for |
45 | 41 | // find the comment with the ID of 823423 |
46 | | - |
47 | | - |
48 | | - const comment = comments.find(comment => comment.id === 823423); |
49 | | - |
| 42 | + const comment = comments.find(comment => comment.id === 823423) |
50 | 43 | console.log(comment); |
51 | 44 |
|
52 | 45 | // Array.prototype.findIndex() |
53 | 46 | // Find the comment with this ID |
54 | 47 | // delete the comment with the ID of 823423 |
55 | | - const index = comments.findIndex(comment => comment.id === 823423); |
| 48 | + const index = comments.findIndex(comment => comment.id === 823423) |
56 | 49 | console.log(index); |
57 | 50 |
|
58 | | - // comments.splice(index, 1); |
59 | | - |
60 | 51 | const newComments = [ |
61 | 52 | ...comments.slice(0, index), |
62 | 53 | ...comments.slice(index + 1) |
63 | | - ]; |
| 54 | + ] |
| 55 | + console.table(newComments) |
64 | 56 |
|
65 | 57 | </script> |
66 | 58 | </body> |
|
0 commit comments