Question: here is my code: // An Email Simulation /* create your email class here */ class Email { constructor(fromAddress, emailContents) { this.fromAddress = fromAddress; this.emailContents
here is my code:
// An Email Simulation /* create your email class here */ class Email { constructor(fromAddress, emailContents) { this.fromAddress = fromAddress; this.emailContents = emailContents; this.hasBeenRead = false; this.isSpam = false; } markAsRead() { this.hasBeenRead = true; } markAsSpam() { this.isSpam = true; } } const inbox = []; function addEmail(contents, address) { const email = new Email(address, contents); inbox.push(email); } function getCount() { return inbox.length; } function getEmail(index) { const email = inbox[index]; email.markAsRead(); return email.emailContents; } function getUnreadEmails() { return inbox.filter(email => !email.hasBeenRead); } function getSpamEmails() { return inbox.filter(email => email.isSpam); } function deleteEmail(index) { inbox.splice(index, 1); } let userChoice = ""; while (userChoice !== "7") { userChoice = prompt( "What would you like to do: 1. Read email 2. Mark spam 3. Send email 4. Delete email 5. View spam emails 6. View unread emails 7. quit?" ); if (userChoice === "1") { const index = prompt(`Enter index of email (0-${getCount() - 1}):`); console.log(getEmail(index)); } else if (userChoice === "2") { const index = prompt(`Enter index of email (0-${getCount() - 1}):`); inbox[index].markAsSpam(); console.log(`Email at index ${index} marked as spam.`); } else if (userChoice === "3") { const contents = prompt("Enter email contents:"); const address = prompt("Enter sender email address:"); addEmail(contents, address); console.log("Email added to inbox."); } else if (userChoice === "4") { const index = prompt(`Enter index of email (0-${getCount() - 1}):`); deleteEmail(index); console.log(`Email at index ${index} deleted from inbox.`); } else if (userChoice === "5") { console.log("Spam emails:"); console.log(getSpamEmails()); } else if (userChoice === "6") { console.log("Unread emails:"); console.log(getUnreadEmails()); } else if (userChoice === "7") { console.log("Goodbye"); } else { console.log("Oops - incorrect input"); } }
lecturers review: You have not instantiated your Class so your program has nothing to work with. You need to create a couple of Email instances and this will be your objects that you will be working with.
Please help me fix my code to the lecturers comments
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
