|
25 | 25 | ]; |
26 | 26 |
|
27 | 27 | // Some and Every Checks |
28 | | - // Array.prototype.some() // is at least one person 19 or older? |
29 | | - const someAdult = people.some(person => { |
30 | | - const currentYear = new Date().getFullYear() |
31 | | - //console.log(currentYear - person.year) |
32 | | - return currentYear - person.year >= 19 |
33 | | - }) |
| 28 | + // Array.prototype.some() |
| 29 | + // is at least one person 19 or older? |
| 30 | + const currentYear = new Date().getFullYear() |
| 31 | + //console.log(currentYear) |
| 32 | + const someAdult = people.some(person => (currentYear - person.year) >= 19) |
34 | 33 | //console.log(someAdult) |
35 | 34 |
|
36 | | - // Array.prototype.every() // is everyone 19 or older? |
37 | | - const allAdults = people.every(person => { |
38 | | - const currentYear = new Date().getFullYear() |
39 | | - //console.log(currentYear - person.year >= 19) |
40 | | - return currentYear - person.year >= 19 |
41 | | - }) |
| 35 | + |
| 36 | + // Array.prototype.every() |
| 37 | + // is everyone 19 or older? |
| 38 | + const currentY = new Date().getFullYear() |
| 39 | + //console.log(currentY) |
| 40 | + const allAdults = people.every(person => (currentY - person.year) >= 19) |
42 | 41 | //console.log(allAdults) |
43 | 42 |
|
| 43 | + |
44 | 44 | // Array.prototype.find() |
45 | 45 | // Find is like filter, but instead returns just the one you are looking for |
46 | 46 | // find the comment with the ID of 823423 |
47 | 47 | const comment = comments.find(c => c.id == 823423) |
48 | | - //console.table(comment) |
| 48 | + //console.log(comment) |
| 49 | + |
49 | 50 |
|
50 | 51 | // Array.prototype.findIndex() |
51 | | - const index = comments.findIndex(c => c.id == 823423) |
52 | | - //console.log(index) |
53 | 52 | // Find the comment with this ID |
54 | | - // delete the comment with the ID of 823423 |
55 | | - // const newComments = comments.splice(index, 1) |
56 | | - // console.table(newComments) |
57 | | - // console.table(comments) |
58 | | - |
59 | | - const newComm = [ |
60 | | - ...comments.slice(0, index), |
61 | | - ...comments.slice(index+1) |
62 | | - ] |
63 | | - console.table(newComm) |
| 53 | + const cIndex = comments.findIndex(c => c.id == 823423) |
| 54 | + //console.log(cIndex) |
| 55 | + |
| 56 | + var array = [ {'a': '12', 'b':'10'}, {'a': '20', 'b':'22'} ]; |
| 57 | + var newArray = array.map(x => { |
| 58 | + x.c = Number(x.a) - Number(x.b) |
| 59 | + x.d = Math.floor(x.a) - Math.floor(x.b) |
| 60 | + return x |
| 61 | + }) |
| 62 | + //console.log(newArray) |
| 63 | + |
| 64 | + //Filter the even numbers |
| 65 | + const arrayOfNumbers = [1,2,3,4,5,6,7,8,9] |
| 66 | + const arrayOfEvenNumbers = arrayOfNumbers.filter(n => n%2 === 0) |
| 67 | + //console.log(arrayOfEvenNumbers) |
| 68 | + const sumOfNumbers = arrayOfNumbers.reduce((acc, num) => { |
| 69 | + return acc + num |
| 70 | + }, 0) |
| 71 | + //console.log(sumOfNumbers) |
| 72 | + |
| 73 | + //Do a simple string search |
| 74 | + var strings = ["hello", "Matt", "Mastodon", "Cat", "Dog"]; |
| 75 | + const stringIncludes = strings.filter(s => s.includes('at')) |
| 76 | + //console.log(stringIncludes) |
| 77 | + |
| 78 | +const spotifySongs = [ |
| 79 | + { id: 1, name: "Curl of the Burl", artist: "Mastodon", duration: 204 }, |
| 80 | + { id: 2, name: "Oblivion", artist: "Mastodon", duration: 306 }, |
| 81 | + { id: 3, name: "Flying Whales", artist: "Gojira", duration: 444 }, |
| 82 | + { id: 4, name: "L'Enfant Sauvage", artist: "Gojira", duration: 246 } |
| 83 | +]; |
| 84 | + |
| 85 | +const lastFmSongs = [ |
| 86 | + { id: 5, name: "Chop Suey", artist: "System of a Down", duration: 192 }, |
| 87 | + { id: 6, name: "Throne", artist: "Bring me the Horizon", duration: 186 }, |
| 88 | + { id: 7, name: "Destrier", artist: "Agent Fresco", duration: 132 }, |
| 89 | + { id: 8, name: "Out of the Black", artist: "Royal Blood", duration: 240 } |
| 90 | +]; |
| 91 | + |
| 92 | +const allSongs = [...spotifySongs, ...lastFmSongs]; |
| 93 | + |
| 94 | +//Transforming the given array and adding and removing properties from each object |
| 95 | +var listOfSongs = allSongs.map(song => { |
| 96 | + let {artist, ...noArtist} = song |
| 97 | + // Object.assign(noArtist, { |
| 98 | + // year: 2010 |
| 99 | + // }) |
| 100 | + return { |
| 101 | + ...noArtist, |
| 102 | + year: 2011 |
| 103 | + } |
| 104 | +}) |
| 105 | +//console.log(listOfSongs) |
| 106 | + |
| 107 | +const listOfSongsFiltered = allSongs.filter(s => s.artist.toLowerCase() == 'mastodon') |
| 108 | +//console.log(listOfSongsFiltered) |
| 109 | + |
| 110 | +const listOfArtists = allSongs.reduce((acc, val) => { |
| 111 | + let currentArtist = val.artist |
| 112 | + let artistsSongs = acc[currentArtist] ? (acc[currentArtist] + 1) : 1 |
| 113 | + return { |
| 114 | + ...acc, |
| 115 | + [currentArtist]: artistsSongs |
| 116 | + } |
| 117 | +},{}) |
| 118 | +//console.log(listOfArtists) |
| 119 | + |
| 120 | +//Difference between let and var |
| 121 | +for (let i = 0; i < 3; i++) { |
| 122 | + //setTimeout( () => console.log('let=' + i), 3000) |
| 123 | + //console.log('var=' + i) |
| 124 | +} |
| 125 | + |
| 126 | +//Now we must get a string separated by commas with all the songs that have a duration superior to 3 minutes. |
| 127 | +const arrayOfLongSongs = allSongs.filter(song => (song.duration/60) > 3).map(s => s.name ) |
| 128 | +console.log(arrayOfLongSongs.join(', ')) |
| 129 | + |
64 | 130 | </script> |
65 | 131 | </body> |
66 | 132 | </html> |
0 commit comments