Created
April 14, 2020 01:40
-
-
Save Nicknyr/db040a418e5f803693520103133fba51 to your computer and use it in GitHub Desktop.
CodeSignal - Find Email Domain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
An email address such as "[email protected]" is made up of a local part ("John.Smith"), an "@" symbol, then a domain part ("example.com"). | |
The domain name part of an email address may only consist of letters, digits, hyphens and dots. The local part, however, also allows a lot of different special characters. Here you can look at several examples of correct and incorrect email addresses. | |
Given a valid email address, find its domain part. | |
Example | |
For address = "[email protected]", the output should be | |
findEmailDomain(address) = "example.com"; | |
For address = "[email protected]", the output should be | |
findEmailDomain(address) = "codesignal.com". | |
*/ | |
function findEmailDomain(address) { | |
// Turn string into array of letters | |
let arr = address.split(''); | |
let domain = ''; | |
let sliced = ''; | |
// Iterate through array checking if the current element is @ | |
// If current item is @, slice everything after the @ | |
for(let i = 0; i < arr.length; i++) { | |
if(arr[i].charCodeAt(0) === 64) { | |
sliced = arr.slice(i + 1); | |
} | |
} | |
// Turn array containing domain info back into a string | |
domain = sliced.join(''); | |
return domain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment