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