|
| 1 | +const answer1 = document.querySelector('.answer1'); |
| 2 | +const answer2 = document.querySelector('.answer2'); |
| 3 | +const answer3 = document.querySelector('.answer3'); |
| 4 | +const answer4 = document.querySelector('.answer4'); |
| 5 | + |
| 6 | +const people = [ |
| 7 | + { name: 'Wes', year: 1988 }, |
| 8 | + { name: 'Kait', year: 1986 }, |
| 9 | + { name: 'Irv', year: 1970 }, |
| 10 | + { name: 'Lux', year: 2015 } |
| 11 | +]; |
| 12 | + |
| 13 | +const comments = [ |
| 14 | + { text: 'Love this!', id: 523423 }, |
| 15 | + { text: 'Super good', id: 823423 }, |
| 16 | + { text: 'You are the best', id: 2039842 }, |
| 17 | + { text: 'Ramen is my fav food ever', id: 123523 }, |
| 18 | + { text: 'Nice Nice Nice!', id: 542328 } |
| 19 | +]; |
| 20 | + |
| 21 | +// Some and Every Checks |
| 22 | +// Array.prototype.some() // is at least one person 19 or older? |
| 23 | +// Array.prototype.every() // is everyone 19 or older? |
| 24 | +const isAudlt = people.some(person => (new Date()).getFullYear() - person.year >= 19); |
| 25 | +console.log(`is at least one person 19 or older? ${isAudlt}`); |
| 26 | +answer1.innerHTML = isAudlt; |
| 27 | +const allAdult = people.every(person => (new Date()).getFullYear() - person.year >= 19); |
| 28 | +console.log(`is everyone 19 or older? ${allAdult}`); |
| 29 | +answer2.innerHTML = allAdult; |
| 30 | + |
| 31 | +// Array.prototype.find() |
| 32 | +// Find is like filter, but instead returns just the one you are looking for |
| 33 | +// find the comment with the ID of 823423 |
| 34 | +const findComment = comments.find(comment => comment.id === 823423); |
| 35 | +console.log(findComment); |
| 36 | +answer3.innerHTML = `text: ${findComment.text}, id: ${findComment.id}`; |
| 37 | + |
| 38 | +// Array.prototype.findIndex() |
| 39 | +// Find the comment with this ID |
| 40 | +// delete the comment with the ID of 823423 |
| 41 | +const findCommentIndex = comments.findIndex(comment => comment.id === 823423); |
| 42 | +console.log(`findCommentIndex: ${findCommentIndex}`); |
| 43 | +comments.splice(findCommentIndex, 1); |
| 44 | +console.log(comments); |
| 45 | +answer4.innerHTML = JSON.stringify(comments) |
0 commit comments