I’m currently working on a Lab about classes. It’s been some time since I’ve worked with classes and I am confused on how I can access the current object’s properties from another class. I need to return a string that prints out the ‘ProjectIdea title’ as well as ‘ProjectIdea status description’. I am unsure if I should be extending the ‘ProjectIdea’ class to inherit it’s properties then use them in the ‘ProjectIdeaBoard’ class or if I should be doing something differently.
Your code so far
const projectStatus = {
PENDING: {description: "Pending Execution"},
SUCCESS: {description: "Executed Successfully"},
FAILURE: {description: "Execution Failed"}
};
class ProjectIdea {
status = projectStatus.PENDING;
constructor(title, description) {
this.title = title;
this.description = description;
}
updateProjectStatus(newStatus) {
this.status = newStatus;
}
}
class ProjectIdeaBoard {
constructor(title) {
this.title = title;
const ideas = [];
}
pin(projectIdeaClass) {
ideas.push(projectIdeaClass);
}
unpin(projectIdeaClass) {
ideas.pop(projectIdeaClass);
}
count() {
return ideas.length;
}
// How can I access the ProjectIdea title if its in a different class?
formatToString() {
return `
${this.title} has ${count()} idea(s)
<ProjectIdea title> (<ProjectIdea status description>) - <ProjectIdea description>
<ProjectIdea title> (<ProjectIdea status description>) - <ProjectIdea description>
.
.
.`
}
}
Current step I’m stuck on:
You should define a method named formatToString that returns the name of the projects ideas, their description and status in the format:
<ProjectIdeaBoard title> has <ProjectIdeaBoard count> idea(s)
<ProjectIdea title> (<ProjectIdea status description>) - <ProjectIdea description>
<ProjectIdea title> (<ProjectIdea status description>) - <ProjectIdea description>
.
.
.
Challenge Information:
Build a Project Idea Board - Build a Project Idea Board
6. Your ProjectIdeaBoard should initialize the title property based on the parameter passed and initialize an empty array named ideas to hold instances of the ProjectIdea class.
Hm, I assume I should still be initializing it within the constructor? I omitted the ‘.this’ keyword for this change, but it doesn’t pass the given test case.
const projectStatus = {
PENDING: {description: "Pending Execution"},
SUCCESS: {description: "Executed Successfully"},
FAILURE: {description: "Execution Failed"}
};
class ProjectIdea {
status = projectStatus.PENDING;
constructor(title, description) {
this.title = title;
this.description = description;
}
updateProjectStatus(newStatus) {
this.status = newStatus;
}
}
class ProjectIdeaBoard {
// Field declaration don't use keywords like 'let' or 'const'
// ideas = [];
constructor(title) {
this.title = title;
ideas = [];
// this.projectIdea = new ProjectIdea();
}
pin(ProjectIdea) {
ideas.push(ProjectIdea);
}
unpin(ProjectIdea) {
ideas.pop(ProjectIdea);
}
count() {
return ideas.length;
}
formatToString() {
string = `${this.title} has ${count()} idea(s)\n`;
for (const element of this.ideas) {
string += `${this.ideas[element].title} (${this.ideas[element].status}) - ${this.ideas[element].description}\n`
}
return string;
}
}
I’ve made the following changes to the formatToString() instance method due to some help on a stack overflow forum post.
Ah, okay I added the ‘this’ keyword back and thanks to some help from some people on stack overflow I was able to make the following revisions to the program:
However, I still appear to be failing 2 test cases:
17. When new ProjectIdeaBoard("Empty Board") is empty, emptyBoard.formatToString() should return Empty Board has 0 idea(s)\n.
18. When you pin new ProjectIdea("Smart Home System", "An integrated system to control lighting, temperature, and security devices remotely.") to new ProjectIdeaBoard("Tech Projects Board"), techProjects.formatToString() should return Tech Projects Board has 1 idea(s)\nSmart Home System (Pending Execution) - An integrated system to control lighting, temperature, and security devices remotely.\n.
I’m curious where else the syntax may be causing these issues or if I am accessing the properties/values incorrectly.