<p>This is the JavaScript code for 08_11 in the exercise files. In this case, we are not using a conditional statement, but we will use one to improve this code.</p>
<h2class="sectiontitle">Backpack.js</h2>
<p>This is the code for the Backpack object</p>
<preclass="codesample">
class Backpack {
constructor(
name,
volume,
color,
pocketNum,
strapLengthL,
strapLengthR,
lidOpen,
dateAcquired,
image
) {
this.name = name;
this.volume = volume;
this.color = color;
this.pocketNum = pocketNum;
this.strapLength = {
left: strapLengthL,
right: strapLengthR,
};
this.lidOpen = lidOpen;
this.dateAcquired = dateAcquired;
this.image = image;
}
toggleLid(lidStatus) {
this.lidOpen = lidStatus;
}
newStrapLength(lengthLeft, lengthRight) {
this.strapLength.left = lengthLeft;
this.strapLength.right = lengthRight;
}
backpackAge() {
let now = new Date();
let acquired = new Date(this.dateAcquired);
let elapsed = now - acquired; // elapsed time in milliseconds
let daysSinceAcquired = Math.floor(elapsed / (1000 * 3600 * 24));
return daysSinceAcquired;
}
}
export default Backpack;</pre>
<h2class="sectiontitle">script.js</h2>
<p>This code creates a backpack object, generates an HTML element to display the object and adds it to the HTML document.</p>
<preclass="codesample">
/**
* Create a new element and append it to the <main> element.