From b5931124097c4929f3a2d0b2c50b6527464e7066 Mon Sep 17 00:00:00 2001 From: gloreea Date: Tue, 14 Mar 2023 08:50:43 -0700 Subject: [PATCH 1/2] 3-13 Deliverable --- exercises.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/exercises.js b/exercises.js index 4ffe65f..31fcd05 100644 --- a/exercises.js +++ b/exercises.js @@ -9,12 +9,16 @@ const album1 = { label: "Sire", formats: ["LP"] } -} +} // 1. Retrieve the string "Sire" from album1, and save it in a sensibly named -// variable. +// variable. +const album1Label = album1.albumDetails.label +console.log (album1Label) // 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", @@ -37,6 +41,7 @@ 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(album2.albumDetails.formats[0]) // 4. Change the release date of album3 from a string into a Date object // Look ahead to album4 for a clue! @@ -48,6 +53,7 @@ const album4 = { formats: ["Cassette", "LP"] } } +album3.albumDetails.released = new Date("August 3, 1979") // 5. Add the label "Sire" to album4's details @@ -58,9 +64,10 @@ const album5 = { label: "Sire" } } +album4.albumDetails.label = "Sire" // 6. Add a 'formats' array to album 5 and add "CD", "Cassette", and "LP" - +album5.albumDetails.formats = ["CD", "Cassette", "LP"] const album6 = { title: "Little Creatures", albumDetails: { @@ -70,8 +77,11 @@ 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() + const album7 = { title: "True Stories", @@ -85,6 +95,7 @@ const album7 = { // 8. Convert album7's 'labels' property from the string value // "Sire, EMI" into the array: ["Sire", "EMI"] // google js array split! +album7.albumDetails.labels = album7.albumDetails.labels.split[","] ///////////////////////////////////////////////////// // Part 2: More Tasks About Datatypes and Structures @@ -111,17 +122,17 @@ const talkingHeadsAlbums = [ ] // 1. Create an object literal called `band`. - // 2. Give it the property `name` and set it to "Talking Heads" - // 3. Give it the property `members` and set it to an array with a single // string, "David Byrne", in it. - // 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.member.push("Chris Franz", "Tiny Weymouth", "Jerry Harrison") +console.log(band) //////////////////////////////////////////////// // Part 3: Conditional Logic @@ -131,11 +142,16 @@ 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. - +if talkingHeadsAlbums.length >= 6 ? { + console.log("Talking Heads were a prolific band") + } else { + 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. +console.log(talkingHeadsAlbums.length %2 === 0 ? `The number ${talkingHeadsAlbums.length} is even` : `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 +162,22 @@ const talkingHeadsAlbums = [ // - "The number Y is not divisible by 2 or 3", // // with Y being the number of albums. - +console.log(talkingHeadsAlbums.length %3 === 0 ? `The number ${talkingHeadsAlbums.length} is divisible by 3` : `The number ${talkingHeadsAlbums.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.logs (talkingHeadsAlbums.length %9 === 0 ? `The number ${talkingHeadsAlbums.length} is divisible by 9` : `The number ${talkingHeadsAlbums.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, // 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! +// have `.labels`, which is an Array! \ No newline at end of file From 9c2cf1a73ef74b43f2311d86f4d6259435bb1120 Mon Sep 17 00:00:00 2001 From: gloreea Date: Tue, 14 Mar 2023 10:06:54 -0700 Subject: [PATCH 2/2] fixed errors and completed --- exercises.js | 74 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/exercises.js b/exercises.js index 31fcd05..37a633e 100644 --- a/exercises.js +++ b/exercises.js @@ -13,11 +13,11 @@ const album1 = { // 1. Retrieve the string "Sire" from album1, and save it in a sensibly named // variable. -const album1Label = album1.albumDetails.label +let album1Label = album1.albumDetails.label console.log (album1Label) // 2. Change the title of album1 from "Talking Heads" to "Talking Heads: 77" -album1.title = "talking Heads: 77" +album1.title = "Talking Heads: 77" console.log(album1.title) const album2 = { @@ -41,7 +41,8 @@ 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(album2.albumDetails.formats[0]) +album3.albumDetails.formats.push("LP") +console.log(album3.albumDeatils.formats) // 4. Change the release date of album3 from a string into a Date object // Look ahead to album4 for a clue! @@ -54,6 +55,7 @@ const album4 = { } } album3.albumDetails.released = new Date("August 3, 1979") +console.log(album3.albumDetails.released) // 5. Add the label "Sire" to album4's details @@ -65,9 +67,12 @@ const album5 = { } } album4.albumDetails.label = "Sire" +console.log(album4.albumDetails.label) // 6. Add a 'formats' array to album 5 and add "CD", "Cassette", and "LP" album5.albumDetails.formats = ["CD", "Cassette", "LP"] +console.log(album5.albumDetails.formats) + const album6 = { title: "Little Creatures", albumDetails: { @@ -81,7 +86,7 @@ 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.albumDetails.labels[1]) const album7 = { title: "True Stories", @@ -95,8 +100,9 @@ const album7 = { // 8. Convert album7's 'labels' property from the string value // "Sire, EMI" into the array: ["Sire", "EMI"] // google js array split! -album7.albumDetails.labels = album7.albumDetails.labels.split[","] - +let album7split = album7.albumDetails.labels.split[","] +album7.albumDetails.labels = album7Split +console.log(album7.albumDetails.labels) ///////////////////////////////////////////////////// // Part 2: More Tasks About Datatypes and Structures ///////////////////////////////////////////////////// @@ -127,11 +133,11 @@ const talkingHeadsAlbums = [ // string, "David Byrne", in it. // 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}; +let 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.member.push("Chris Franz", "Tiny Weymouth", "Jerry Harrison") +band.members.push("Chris Franz", "Tiny Weymouth", "Jerry Harrison") console.log(band) //////////////////////////////////////////////// @@ -151,7 +157,11 @@ if talkingHeadsAlbums.length >= 6 ? { // 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(talkingHeadsAlbums.length %2 === 0 ? `The number ${talkingHeadsAlbums.length} is even` : `The number ${talkingHeadsAlbums.length} is odd`) +if((talkingHeadsAlbums.length %2) === 0) { + console.log(`The number ${talkingHeadsAlbums.length} is even`) + } else { + 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 @@ -162,22 +172,54 @@ console.log(talkingHeadsAlbums.length %2 === 0 ? `The number ${talkingHeadsAlbum // - "The number Y is not divisible by 2 or 3", // // with Y being the number of albums. -console.log(talkingHeadsAlbums.length %3 === 0 ? `The number ${talkingHeadsAlbums.length} is divisible by 3` : `The number ${talkingHeadsAlbums.length} is not divisible by 3`); +if ((talkingHeadsAlbums.length %3 === 0) && (talkingHeadsAlbums.length %2 === 0 )) { + console.log( `The number ${talkingHeadsAlbums.length} is divisible by 2 and 3`) + } else if (talkingHeadsAlbums.length %2 === 0) { + console.log( `The number ${talkingHeadsAlbums.length} is divisible by 2`) + } else if (talkingHeadsAlbums.length %3 ===0) { + console.log(`The number ${talkingHeadsAlbums.length} is divisible by 3`) + } else { + console.log(`The number ${talkingHeadsAlbums.length} is NOT divisible by 2 or 3.`) + } // 4. Check your logic above against the numbers: 0, 1, 2, 6, 7, and 9. // Make sure it always works! -console.logs (talkingHeadsAlbums.length %9 === 0 ? `The number ${talkingHeadsAlbums.length} is divisible by 9` : `The number ${talkingHeadsAlbums.length} is not divisible by 9`); - +let testNumber = 0 +if(testNumber === 0 ) { + console.log("zero") +} else if ((testNumber %3 === 0) && (testNumber %2 === 0 )) { + console.log( `The number ${testNumberh} is divisible by 2 and 3`) + } else if (testNumber %2 === 0) { + console.log( `The number ${testNumber} is divisible by 2`) + } else if (testNumber %3 ===0) { + console.log(`The number ${testNumber} is divisible by 3`) + } else { + console.log(`The number ${testNumber} is NOT divisible by 2 or 3.`) + } ///////////////////////////////////////////////////// // 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) -}) +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! \ No newline at end of file +// have `.labels`, which is an Array! +let sireTally = 0 +for(album in talkingHeadsAlbums) { + if(talkingHeadsAlbums[album].albumDetails.label) { + if(talkingHeadsAlbums[album].albumDetails.label === "Sire" ) { + sireTally++ + } + } else if(talkingHeadsAlbums[album].albumDetails.labels) { + if(talkingHeadsAlbums[album].albumDetails.labels.includes("Sire")) { + sireTally++ + } + } +} +console.log(sireTally) \ No newline at end of file