diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..1dd6511 Binary files /dev/null and b/.DS_Store differ diff --git a/bonus.js b/bonus.js index bf067c4..5896f42 100644 --- a/bonus.js +++ b/bonus.js @@ -1,4 +1,3 @@ - ///////////////////////////////////////////////////// // Bonus: More Tasks With Conditionals and Iteration ///////////////////////////////////////////////////// @@ -41,6 +40,15 @@ const ticketSections = [ {name: "Bob Dole", section: "center", type: "premium", seats: 3} ] +for (let i=0; ticketSections.length; i++) { + const dignitary = ticketSections[i] + if (dignitary.type === "premium") { + console.log(`Welcome, ${dignitary.name}! You may sit anywhere in the first 3 rows of the ${dignitary.section} section.`); + } else { + console.log(`Welcome, ${dignitary.name}! You and your party may sit anywhere except first 3 rows of the ${dignitary.section} section. Please be sure to leave no seats between you.`); + } +} + // 2. There is a concert at the LA County Fairgrounds by the Southland's // hottest Talking Heads tribute band for zombie afficianados, // "The Wailing Deads" (known as "The Walking Deads" until they received @@ -79,4 +87,4 @@ const tickets = [ {amount: 80.00, discount: true}, {amount: 90.00}, {amount: 50.00, discount: true} -] +] \ No newline at end of file diff --git a/exercises.js b/exercises.js index 4ffe65f..0f8c22d 100644 --- a/exercises.js +++ b/exercises.js @@ -1,3 +1,4 @@ + //////////////////////////////////////////////// // Part 1: Working With Data Structures //////////////////////////////////////////////// @@ -10,12 +11,14 @@ const album1 = { formats: ["LP"] } } - // 1. Retrieve the string "Sire" from album1, and save it in a sensibly named // variable. +let albumLabel = album1.albumDetails.label // 2. Change the title of album1 from "Talking Heads" to "Talking Heads: 77" +album1.title = "Talking Heads: 77" + const album2 = { title: "More Songs About Buildings and Food", albumDetails: { @@ -33,13 +36,231 @@ const album3 = { formats: ["Cassette"] } } +// 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! +album3.albumDetails.released = new Date(album3.albumDetails.released) + +const album4 = { + title: "Remain in Light", + albumDetails: { + released: new Date("October 8, 1980"), + formats: ["Cassette", "LP"] + } +} + +// 5. Add the label "Sire" to album4's details +album4.albumDetails.label = "Sire" + + +const album5 = { + title: "Speaking in Tongues", + albumDetails: { + released: new Date("May 31, 1983"), + 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: { + released: new Date("June 10, 1985"), + labels: ["Sire", "emi"], + formats: ["CD", "cassette", "LP"] + } +} + +// 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", + albumDetails: { + released: new Date("October 7, 1986"), + labels: "Sire, EMI", + formats: ["CD", "cassette", "LP"] + } +} + +// 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(", ") +console.log(album7) + +///////////////////////////////////////////////////// +// Part 2: More Tasks About Datatypes and Structures +///////////////////////////////////////////////////// + +const album8 = { + title: "Naked", + albumDetails: { + released: new Date("March 15, 1988"), + labels: ["Sire", "EMI"], + formats: ["CD", "cassette", "LP"] + } +} + +const talkingHeadsAlbums = [ + album1, + album2, + album3, + album4, + album5, + album6, + album7, + album8 +] +// 1. Create an object literal called `band`. +let band = {name:"Talking Heads", members:"David Byrne, Tiny Weymouth, Chris Franz, Jerry Harrison", albums: talkingHeadsAlbums} + +console.log(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 + +// 5. Add "Tiny Weymouth", "Chris Franz" and "Jerry Harrison" to the members +// array. + +//////////////////////////////////////////////// +// Part 3: Conditional Logic +//////////////////////////////////////////////// + +// 1. Write a conditional to console.log "Talking Heads were a prolific band" +// 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. + +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 +// console.log one of: +// - "The number Y is divisible by 2", +// - "The number Y is divisible by 3", +// - "The number Y is divisible by 2 and 3", or +// - "The number Y is not divisible by 2 or 3", +// +// with Y being the number of albums. +const thAlbums = talkingHeadsAlbums.length; + +if (thAlbums % 2 === 0 && thAlbums % 3 === 0) { +console.log(`The number ${thAlbums} is divisible by 2 and 3`); +} else if (thAlbums % 2 === 0) { +console.log(`The number ${thAlbums} is divisible by 2`); +} else if (thAlbums % 3 === 0) { +console.log(`The number ${thAlbums} is divisible by 3`); +} else { +console.log(`The number ${thAlbums} 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! + +if (talkingHeadsAlbums.length % 2 == 0) { + console.log(`The number ${talkingHeadsAlbums.length} is divisible by 2`) +} else { + console.log(`The number ${talkingHeadsAlbums.length} is not divisible by 2`) +} +///////////////////////////////////////////////////// +// Part 4: For Loops +///////////////////////////////////////////////////// + +// 1. Use a for loop to print out the name of each Talking Heads album +// for each + +for (let i = 0; i < talkingHeadsAlbums.length; i++) { + console.log(talkingHeadsAlbums[i].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. +// for each + +let sireTally = 0 + +for (let i = 0; i < talkingHeadsAlbums.length; i++) { + if (talkingHeadsAlbums[i].albumDetails.label === "Sire") { + sireTally++; + } +} + +console.log(sireTally); +// Warning: some albums have a property `.label`, which is a string, and some +// have `.labels`, which is an Array! + +//////////////////////////////////////////////// +// Part 1: Working With Data Structures +//////////////////////////////////////////////// + +const album1 = { + title: "Talking Heads", + albumDetails: { + released: new Date("September 16, 1977"), + label: "Sire", + formats: ["LP"] + } +} + +// 1. Retrieve the string "Sire" from album1, and save it in a sensibly named +// variable. + +let albumLabel = album1.albumDetails.label + +// 2. Change the title of album1 from "Talking Heads" to "Talking Heads: 77" +album1.title = "Talking Heads: 77" + +const album2 = { + title: "More Songs About Buildings and Food", + albumDetails: { + released: new Date("July 14, 1978"), + label: "Sire", + formats: ["LP", "8-track"] + } +} +const album3 = { + title: "Fear of Music", + albumDetails: { + released: "August 3, 1979", + label: "Sire", + formats: ["Cassette"] + } +} // 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! +album3.albumDetails.released = new Date(album3.albumDetails.released) const album4 = { title: "Remain in Light", @@ -50,6 +271,8 @@ const album4 = { } // 5. Add the label "Sire" to album4's details +album4.albumDetails.label = "Sire" + const album5 = { title: "Speaking in Tongues", @@ -60,6 +283,7 @@ const album5 = { } // 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", @@ -72,6 +296,8 @@ 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 +311,8 @@ 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(", ") +console.log(album7) ///////////////////////////////////////////////////// // Part 2: More Tasks About Datatypes and Structures @@ -111,7 +339,9 @@ const talkingHeadsAlbums = [ ] // 1. Create an object literal called `band`. +let band = {name:"Talking Heads", members:"David Byrne, Tiny Weymouth, Chris Franz, Jerry Harrison", albums: talkingHeadsAlbums} +console.log(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 @@ -130,13 +360,26 @@ const talkingHeadsAlbums = [ // 1. Write a conditional to console.log "Talking Heads were a prolific band" // 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. +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 // console.log one of: @@ -147,18 +390,52 @@ const talkingHeadsAlbums = [ // // with Y being the number of albums. +const thAlbums = talkingHeadsAlbums.length; + +if (thAlbums % 2 === 0 && thAlbums % 3 === 0) { +console.log(`The number ${thAlbums} is divisible by 2 and 3`); +} else if (thAlbums % 2 === 0) { +console.log(`The number ${thAlbums} is divisible by 2`); +} else if (thAlbums % 3 === 0) { +console.log(`The number ${thAlbums} is divisible by 3`); +} else { +console.log(`The number ${thAlbums} 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! +if (talkingHeadsAlbums.length % 2 == 0) { + console.log(`The number ${talkingHeadsAlbums.length} is divisible by 2`) +} else { + console.log(`The number ${talkingHeadsAlbums.length} is not divisible by 2`) +} + ///////////////////////////////////////////////////// // Part 4: For Loops ///////////////////////////////////////////////////// // 1. Use a for loop to print out the name of each Talking Heads album +// for each + +for (let i = 0; i < talkingHeadsAlbums.length; i++) { + console.log(talkingHeadsAlbums[i].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. -// +// for each + +let sireTally = 0 + +for (let i = 0; i < talkingHeadsAlbums.length; i++) { + if (talkingHeadsAlbums[i].albumDetails.label === "Sire") { + sireTally++; + } +} + +console.log(sireTally); // Warning: some albums have a property `.label`, which is a string, and some // have `.labels`, which is an Array! +