Day 20 of Scrimba JavaScriptmas — Domain Type

Handhika Yanuar Pratama
2 min readDec 20, 2020

--

Hi this is my writeup for day 20 of #Scrimba #JavaScriptmas challenges. The title for this challenge is domain type. For my POV this is easier than 2 days before. The hints really help much, so before you read this, i hope you get the solution first by yourself.

My solution is not the simpler one, but i think this is the explainable one. So this is my solution for day 20:

const domains = ["en.wiki.org", "codefights.com", "happy.net", "code.info"];function domainType(domains) {
// write code here.
let domainsType = [];
for (i = 0; i < domains.length; i++) {
jem = domains[i].split(".");
if (jem[jem.length - 1] === "org") {
domainsType.push("organization");
}
if (jem[jem.length - 1] === "com") {
domainsType.push("commercial");
}
if (jem[jem.length - 1] === "net") {
domainsType.push("network");
}
if (jem[jem.length - 1] === "info") {
domainsType.push("information");
}
}
}

The question is to translate the top-level-domain (.org, .com, .net, .info) into the type of the domain like this:

  • .org → organization
  • .com → commercial
  • .net → network
  • .info → information

The hints for this question is using split() and push() function. So for the beginning i make an empty array ‘domainsType’. Next, i use for loop to doing looping inside the array of domains. Inside the for loop, i declare new variable jem (‘it just pop up in my opinion, use by your own’). The jem variable is to store, the split of every string in indexed array of i. So, for example “en.wiki.org” become [“en”, “wiki”, “org”].

Because the last index of jem is what we need (the top-level-domain). By using the if statement to make sure the last index is the same with the string that we want to change. To access the last index of array i use ‘jem[jem.length — 1]’. If the name is same, i push the value of the answer into the empty array of domainsType. So the output will be like this.

This is my solution in scrimba

Hope it can help you, thanks for reading.

--

--

Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer