A link's real destination is decided by the end of its host name, not by the words at the front. Phishing hides the real site where people do not look.
What to build
For each link, print the site it really goes to and whether that site is on the trusted list.
Requirements
- Line 1: the trusted sites, separated by spaces.
- Each remaining line is a link. Its host is the part after
://(if present) and before the first/,?or#, with anything up to the last@removed and any:portremoved, in lower case. - Its site is the last two labels of the host (a simplification; real browsers use the Public Suffix List).
- Print
ok SITEif the site is trusted, otherwiseDANGER SITE.
Input
As described.
Output
One line per link.
Constraints
1 to 10 trusted sites and up to 30 links.
Example
Input:
bank.example
https://bank.example/login
https://bank.example.secure-check.net/login
Output:
ok bank.example
DANGER secure-check.net
Why it works
Everything before the last two labels is chosen freely by whoever owns those two labels, so only the end of the host tells you who runs the site.
Examples
Example 1 · Input
bank.example https://bank.example/login https://bank.example.secure-check.net/login
Expected output
ok bank.example DANGER secure-check.net
Example 2 · Input
bank.example https://bank.example@verify-now.net/
Expected output
DANGER verify-now.net
Hints
Hint 1
Split step by step: scheme, then path, then the @, then the port.
Solution
Show one correct solution (try it yourself first)
function hostOf(url) {
const afterScheme = url.includes("://") ? url.split("://")[1] : url;
return afterScheme.split("/")[0].split("?")[0].split("#")[0].split("@").pop().split(":")[0].toLowerCase();
}
const site = (host) => host.split(".").slice(-2).join(".");
const trusted = prompt().trim().split(/\s+/);
let line = prompt();
while (line !== null) {
if (line.trim()) {
const s = site(hostOf(line.trim()));
console.log(`${trusted.includes(s) ? "ok" : "DANGER"} ${s}`);
}
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step