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; }
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; }
Both methods are good for memory muscles, but I found out that the first one most likely going to be the common case.
Top comments (2)
Brilliant article series you're doing here, Sekti! I love it! 💯
Thanks per, the JavaScriptmas Challenge and the Bootcamp help me a ton.