Port numbers tell a computer which service a packet is for. A handful of well-known ports appear in every firewall rule list.
What to build
For each port number, print the service usually found there, or a description of its range.
Requirements
- Known ports: 20 and 21 FTP, 22 SSH, 23 Telnet, 25 SMTP, 53 DNS, 80 HTTP, 110 POP3, 143 IMAP, 443 HTTPS, 3306 MySQL, 3389 RDP, 5432 PostgreSQL.
- Print
PORT namefor a known port. - Otherwise print
PORT well-knownfor 0 to 1023,PORT registeredfor 1024 to 49151, orPORT dynamicfor 49152 to 65535. - A number outside 0 to 65535 prints
PORT invalid.
Input
One port number per line.
Output
One line per port.
Constraints
1 to 50 lines; each is a whole number between −100000 and 100000.
Example
Input:
22
443
8080
Output:
22 SSH
443 HTTPS
8080 registered
Why it works
A lookup table answers the famous ports, and the three IANA ranges describe every other valid port, so every number gets exactly one answer.
Examples
Example 1 · Input
22 443 8080
Expected output
22 SSH 443 HTTPS 8080 registered
Example 2 · Input
3306 60000
Expected output
3306 MySQL 60000 dynamic
Hints
Hint 1
Put the known ports in an object: { 22: "SSH", 80: "HTTP", ... }.
Hint 2
Check the table first, then the ranges.
Solution
Show one correct solution (try it yourself first)
const known = { 20: "FTP", 21: "FTP", 22: "SSH", 23: "Telnet", 25: "SMTP", 53: "DNS", 80: "HTTP", 110: "POP3", 143: "IMAP", 443: "HTTPS", 3306: "MySQL", 3389: "RDP", 5432: "PostgreSQL" };
let line = prompt();
while (line !== null) {
const port = Number(line);
let what;
if (port < 0 || port > 65535) what = "invalid";
else if (known[port]) what = known[port];
else if (port <= 1023) what = "well-known";
else if (port <= 49151) what = "registered";
else what = "dynamic";
console.log(`${port} ${what}`);
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step