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