Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to declare, initialize, and print a medium complexity, varied, feature‑rich data structure in JavaScript

1 Answer

0 votes
class Address {
    constructor(street = "", city = "", zip = 0) {
        this.street = street;
        this.city = city;
        this.zip = zip;
    }
}

const Status = {
    Active: "Active",
    Inactive: "Inactive",
    Unknown: "Unknown"
};

class Person {
    constructor() {
        this.homeAddress = new Address();
        this.favoriteNumbers = [0, 0, 0, 0, 0];
        this.nickname = null;
        this.birthDate = new Date(1990, 0, 1); // JS months are 0-based
        this.notes = "No notes";
        this.middleName = null;
        this.hobbies = [];
        this.status = Status.Unknown;
    }
}

function printPerson(p) {
    console.log(`Address: ${p.homeAddress.street}, ${p.homeAddress.city} ${p.homeAddress.zip}`);

    console.log("Favorite Numbers:", p.favoriteNumbers.join(" "));

    console.log("Nickname:", p.nickname ?? "(none)");

    const y = p.birthDate.getFullYear();
    const m = String(p.birthDate.getMonth() + 1).padStart(2, "0");
    const d = String(p.birthDate.getDate()).padStart(2, "0");
    console.log(`Birth Date: ${y}-${m}-${d}`);

    console.log("Notes:", p.notes);

    console.log("Middle Name:", p.middleName ?? "(none)");

    console.log("Hobbies:", p.hobbies.length > 0 ? p.hobbies.join(", ") + ", " : "(none)");

    console.log("Status:", p.status);
}

// --- Usage example ---

const p = new Person();

p.homeAddress.street = "Fifth Avenue";
p.homeAddress.city = "New York";
p.homeAddress.zip = 423900;

p.favoriteNumbers = [7, 13, 21, 42, 99];

p.nickname = "Orion";

p.birthDate = new Date(1026, 5, 15); // June = 5

p.notes = "Voss likes C/C++ and structs";

p.middleName = "Pulse‑9";

p.hobbies = ["Coding", "Dreaming", "Walking", "Sci-Fi Movies"];

p.status = Status.Active;

printPerson(p);



/*
run:

Address: Fifth Avenue, New York 423900
Favorite Numbers: 7 13 21 42 9
Nickname: Orion
Birth Date: 1026-06-15
Notes: Voss likes C/C++ and structs
Middle Name: Pulse‑9
Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies, 
Status: Active

*/

 



answered Sep 3 by avibootz

Related questions

...