diff --git a/exercises.js b/exercises.js index 4ffe65f..ebfae76 100644 --- a/exercises.js +++ b/exercises.js @@ -13,8 +13,12 @@ const album1 = { // 1. Retrieve the string "Sire" from album1, and save it in a sensibly named // variable. +let albumOneLabel = album1.albumDetails.label; +console.log(albumOneLabel); // 2. Change the title of album1 from "Talking Heads" to "Talking Heads: 77" +albumOneLabel = "Talking Heads: 77"; +console.log(albumOneLabel); const album2 = { title: "More Songs About Buildings and Food", @@ -37,10 +41,17 @@ const album3 = { // 3. Access album2's formats array and use an array method to add "LP" to // album3's formats // Check out the Array.push method! +album3.albumDetails.formats.push("LP"); +console.log(album3); // 4. Change the release date of album3 from a string into a Date object // Look ahead to album4 for a clue! +let album3date = new Date("October 31, 1979"); +album3.albumDetails.released = album3date +console.log(album3) + + const album4 = { title: "Remain in Light", albumDetails: { @@ -48,8 +59,13 @@ const album4 = { formats: ["Cassette", "LP"] } } - // 5. Add the label "Sire" to album4's details +album4.albumDetails = { + label: "Sire", + released: new Date("October 8, 1980"), + formats: ["Cassette", "LP"] +} +console.log(album4) const album5 = { title: "Speaking in Tongues", @@ -60,6 +76,9 @@ const album5 = { } // 6. Add a 'formats' array to album 5 and add "CD", "Cassette", and "LP" +album5.albumDetails.formats = ["CD, Cassette, LP"] +console.log(album5) + const album6 = { title: "Little Creatures", @@ -70,8 +89,14 @@ const album6 = { } } + + // 7. Make the label "emi" in album6 all uppercase // google how to make a string uppercase in js! +album6.albumDetails.labels[1] = album6.albumDetails.labels[1].toUpperCase(); +console.log(album6); + + const album7 = { title: "True Stories", @@ -85,6 +110,13 @@ const album7 = { // 8. Convert album7's 'labels' property from the string value // "Sire, EMI" into the array: ["Sire", "EMI"] // google js array split! +let comma = ","; +// console.log(album7.albumDetails.labels); +album7.albumDetails.labels = album7.albumDetails.labels.split(comma); +console.log(album7); + + + ///////////////////////////////////////////////////// // Part 2: More Tasks About Datatypes and Structures @@ -119,10 +151,13 @@ const talkingHeadsAlbums = [ // 4. Give it the property `albums` and set it to the array stored in the // variable talkingHeadsAlbums + const band = {name: "Talking Heads", members: ["David Byrne"], albums: talkingHeadsAlbums}; +console.log(band); // 5. Add "Tiny Weymouth", "Chris Franz" and "Jerry Harrison" to the members // array. - +band.members.push("Chris Franz", "Tiny Weymouth", "Jerry Harrison"); +console.log(band); //////////////////////////////////////////////// // Part 3: Conditional Logic //////////////////////////////////////////////// @@ -131,11 +166,14 @@ const talkingHeadsAlbums = [ // if the Talking Heads have 6 albums or more. Otherwise, console.log // "Talking heads didn't have much output." Use the array of albums // talkingHeadsAlbums above. +console.log(band.albums.length >= 6 ? `${band.name} were a proflific band` : `${band.name} didn't have much output.`); + // 2. Write a conditional to check if the number of albums in // talkingHeadsAlbums is odd or even, and then console.log // "The number X is odd" or "The number X is even" with X being // the number of albums. +console.log(band.albums.length %2 === 0 ? `The number ${band.albums.length} is even` : `The number ${band.albums.length} is odd`); // 3. Write conditionals to check if the number of albums in // talkingHeadsAlbums is divisible by either 2 or 3, and then @@ -146,15 +184,22 @@ const talkingHeadsAlbums = [ // - "The number Y is not divisible by 2 or 3", // // with Y being the number of albums. +console.log(band.albums.length %3 === 0 ? `The number ${band.albums.length} is divisible by 3` : `The number ${band.albums.length} is not divisible by 3`); + // 4. Check your logic above against the numbers: 0, 1, 2, 6, 7, and 9. // Make sure it always works! +console.log(band.albums.length %9 === 0 ? `The number ${band.albums.length} is divisible by 9` : `The number ${band.albums.length} is not divisible by 9`); ///////////////////////////////////////////////////// // Part 4: For Loops ///////////////////////////////////////////////////// // 1. Use a for loop to print out the name of each Talking Heads album +talkingHeadsAlbums.forEach(function (album) { + console.log(album.title) +}) + // 2. Create a variable called `sireTally`, and set it to the integer value 0. // Then use a for-loop to go through all the Talking Heads albums, @@ -162,3 +207,12 @@ const talkingHeadsAlbums = [ // // Warning: some albums have a property `.label`, which is a string, and some // have `.labels`, which is an Array! + +let sireTally = 0; +talkingHeadsAlbums.forEach(function (album) { + if (talkingHeadsAlbums.label = "Sire") { + sireTally++; + } else if (talkingHeadsAlbums.labels[i] = "Sire") { + sireTally++; + } else {} +})