DEV Community

Cover image for Day 20 of JavaScriptmas - Domain Type Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on

4 1

Day 20 of JavaScriptmas - Domain Type Solution

Day 20 challenge is to return an array of information from the domain name based on the last dot (.)

For example, an array of ["en.wiki.org", "codefights.com", "happy.net", "code.info"] will return ["organization", "commercial", "network", "information"]

I also mapping each domain name .com for commercial, .info for information, .net for network, .org for organization.

These are two methods that I found to solve the challenge

1st - Mapping domain with filter

function domainType(domains) {
    let typeNames = [
        {id: 'com', desc: 'commercial'},
        {id: 'info', desc: 'information'},
        {id: 'net', desc: 'network'},
        {id: 'org', desc: 'organization'}
    ];

    const result = domains.map( url => {
        const urlType = url.split('.').pop();       

        let name = typeNames.filter(name => {
           if(urlType === name.id) {
                return name;
           } 
        });
        name = Object.assign({}, ...name);

        return name.desc;
    });

    return result;
}
Enter fullscreen mode Exit fullscreen mode

2nd - Mapping domain with switch

function domainType(domains) {
    let result = domains.map(domain => {
        switch(domain.split('.').pop()) {
            case 'com':
                return 'commercial';
                break;
            case 'info':
                return 'information';
                break;
            case 'net':
                return 'network';
                break;
            case 'org':
                return 'organization';
                break;
            default: 
             return 'unknown domain';
        }
    });
    return result;
}
Enter fullscreen mode Exit fullscreen mode

Both methods are good for memory muscles, but I found out that the first one most likely going to be the common case.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (2)

Collapse
 
perborgen profile image
Per

Brilliant article series you're doing here, Sekti! I love it! 💯

Collapse
 
flyingduck92 profile image
Sekti Wicaksono

Thanks per, the JavaScriptmas Challenge and the Bootcamp help me a ton.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs