diff --git a/exercises.js b/exercises.js index 4ffe65f..340a727 100644 --- a/exercises.js +++ b/exercises.js @@ -14,8 +14,14 @@ const album1 = { // 1. Retrieve the string "Sire" from album1, and save it in a sensibly named // variable. +let lables = album1.albumDetails.label +console.log(lables) + // 2. Change the title of album1 from "Talking Heads" to "Talking Heads: 77" +album1.title = "Talking Heads: 77" +console.log(album1.title) + const album2 = { title: "More Songs About Buildings and Food", albumDetails: { @@ -38,9 +44,16 @@ const album3 = { // album3's formats // Check out the Array.push method! +console.log(album2.albumDetails.formats) +album3.albumDetails.formats.unshift("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! +album3.albumDetails.released = new Date ("August 3, 1979") +console.log(album3) + const album4 = { title: "Remain in Light", albumDetails: { @@ -51,6 +64,9 @@ const album4 = { // 5. Add the label "Sire" to album4's details +album4.albumDetails.label = "Sire" +console.log(album4) + const album5 = { title: "Speaking in Tongues", albumDetails: { @@ -61,6 +77,9 @@ const album5 = { // 6. Add a 'formats' array to album 5 and add "CD", "Cassette", and "LP" +album5.formats = ['CD', 'Cassette', 'LP'] +console.log(album5) + const album6 = { title: "Little Creatures", albumDetails: { @@ -73,6 +92,9 @@ 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].toLocaleUpperCase() +console.log(album6) + const album7 = { title: "True Stories", albumDetails: { @@ -86,6 +108,9 @@ const album7 = { // "Sire, EMI" into the array: ["Sire", "EMI"] // google js array split! +album7.albumDetails.labels = album7.albumDetails.labels.split(",") +console.log(album7) + ///////////////////////////////////////////////////// // Part 2: More Tasks About Datatypes and Structures ///////////////////////////////////////////////////// @@ -99,6 +124,7 @@ const album8 = { } } + const talkingHeadsAlbums = [ album1, album2, @@ -107,22 +133,28 @@ const talkingHeadsAlbums = [ album5, album6, album7, - album8 + album8, ] // 1. Create an object literal called `band`. +const band = {} + // 2. Give it the property `name` and set it to "Talking Heads" - +band.name = 'Talking Heads' +console.log(band) // 3. Give it the property `members` and set it to an array with a single // string, "David Byrne", in it. - +band.members = 'David Byrn' +console.log(band) // 4. Give it the property `albums` and set it to the array stored in the // variable talkingHeadsAlbums - +band.albums = talkingHeadsAlbums +console.log(band) // 5. Add "Tiny Weymouth", "Chris Franz" and "Jerry Harrison" to the members // array. - +band.members = ['David Byrn', 'Tiny Weymouth', 'Chris Franz', 'Jerry Harrison'] +console.log(band) //////////////////////////////////////////////// // Part 3: Conditional Logic //////////////////////////////////////////////// @@ -132,10 +164,17 @@ const talkingHeadsAlbums = [ // "Talking heads didn't have much output." Use the array of albums // talkingHeadsAlbums above. +talkingHeadsAlbums.length > 5 ? console.log("Talking Heads were a prolific band") : console.log("Talking heads 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. +if(talkingHeadsAlbums.length % 2 === 0 && talkingHeadsAlbums.length !== 0 ) { + console.log(`The number ${talkingHeadsAlbums.length} is even`) + } else if(talkingHeadsAlbums.length % 2 !== 0 && talkingHeadsAlbums.length !== 0 ) { + console.log(`The number ${talkingHeadsAlbums.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,19 +185,54 @@ const talkingHeadsAlbums = [ // - "The number Y is not divisible by 2 or 3", // // with Y being the number of albums. +switch(true) { + case talkingHeadsAlbums.length % 2 === 0 && talkingHeadsAlbums.length % 3 !== 0 && talkingHeadsAlbums.length !== 0: + console.log(`The number ${talkingHeadsAlbums.length} is divisible by 2`) + break; + case talkingHeadsAlbums.length % 3 === 0 && talkingHeadsAlbums.length % 2 !== 0 && talkingHeadsAlbums.length !== 0: + console.log(`The number ${talkingHeadsAlbums.length} is divisible by 3`) + break; + case talkingHeadsAlbums.length % 2 === 0 && talkingHeadsAlbums.length % 3 === 0 && talkingHeadsAlbums.length !== 0: + console.log(`The number ${talkingHeadsAlbums.length} is divisible by 2 and 3`) + break; + case talkingHeadsAlbums.length % 2 !== 0 && talkingHeadsAlbums.length % 3 !== 0 && talkingHeadsAlbums.length !== 0: + console.log(`The number ${talkingHeadsAlbums.length} is not divisible by 2 or 3`) + break; +} + // 4. Check your logic above against the numbers: 0, 1, 2, 6, 7, and 9. // Make sure it always works! +console.log("it all worked checked with the above") + ///////////////////////////////////////////////////// // Part 4: For Loops ///////////////////////////////////////////////////// // 1. Use a for loop to print out the name of each Talking Heads album - + for (i = 0 ; i < talkingHeadsAlbums.length ; i++) { + console.log(talkingHeadsAlbums[i].title) + } + + for (album in talkingHeadsAlbums) { + console.log(talkingHeadsAlbums[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, // incrementing sireTally if the album was released under the "Sire" label. // // Warning: some albums have a property `.label`, which is a string, and some // have `.labels`, which is an Array! + +let sireTally = 0; + +for (let i = 0; i < talkingHeadsAlbums.length; i++) { + if (talkingHeadsAlbums[i].albumDetails.labels = "Sire") { + sireTally++; + } + else if(talkingHeadsAlbums[i].albumDetails.label = "Sire") { + sireTally++; + } +} +console.log(sireTally); \ No newline at end of file