-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetectBrowser.js
22 lines (20 loc) · 1012 Bytes
/
detectBrowser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// You need to detect what browser is being used. Create a function that takes a string (browser identifier) and returns the browser name.
// Examples:
// detectBrowser("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36") ➞ "Google Chrome"
// detectBrowser("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0") ➞ "Mozilla Firefox"
// detectBrowser("Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727;
// .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko") ➞ "Internet Explorer"
function detectBrowser(userAgent) {
if (userAgent.includes("Chrome")) {
return "Google Chrome";
} else if (userAgent.includes("Firefox")) {
return "Mozilla Firefox";
} else {
return "Internet Explorer";
}
}
console.log(
detectBrowser(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"
)
);