Skip to content

Instantly share code, notes, and snippets.

@codebubb
Last active October 11, 2023 19:04
Show Gist options
  • Select an option

  • Save codebubb/9ebc1688ae0a4a4e2643938c2ba08351 to your computer and use it in GitHub Desktop.

Select an option

Save codebubb/9ebc1688ae0a4a4e2643938c2ba08351 to your computer and use it in GitHub Desktop.
Mock Users
const users = [{
id: "e0aafb31-8ac6-48d4-99ee-12af8ddcbdae",
email: "[email protected]",
ip_address: "53.215.156.73"
}, {
id: "a7b0221d-5228-4992-a964-b5f3e3d7a022",
email: "[email protected]",
ip_address: "34.81.219.4"
}, {
id: "1019b52c-db9d-45a2-975f-404455925a6a",
email: "[email protected]",
ip_address: "65.181.114.151"
}, {
id: "e8637c3b-645d-4742-a463-d2006742606f",
email: "[email protected]",
ip_address: "136.171.217.116"
}, {
id: "a20803f2-c372-4f39-97b1-d8e948151c8c",
email: "[email protected]",
ip_address: "220.228.4.97"
}, {
id: "63f8e789-009d-4e7b-ad6b-cc8c8a1d1248",
email: "[email protected]",
ip_address: "101.212.0.49"
}, {
id: "74120248-03b5-4888-84c0-d1a459c6273b",
email: "[email protected]",
ip_address: "28.180.205.167"
}, {
id: "412a0669-7171-48b4-95eb-e898993ec1dc",
email: "[email protected]",
ip_address: "61.44.195.62"
}, {
id: "6cd04ecc-8316-4542-9faa-b8438e82b4ad",
email: "[email protected]",
ip_address: "1.176.238.27"
}, {
id: "cfdf5009-84d3-4599-88c0-048c4fcae2ed",
email: "[email protected]",
ip_address: "44.37.119.76"
}];
/**
* Exercise 01
*
* Check whether every email address in the list of users is valid (get a true or false value)
*/
/**
* Exercise 02
*
* Find out how many users have an IP address in a Class A network
* https://en.wikipedia.org/wiki/Classful_network
*/
/**
* Exercise 03
*
* Find the position in the array of the first user object that has a Class B IP address
*/
/**
* Exercise 04
*
* Find the user object that has the 'soundcloud' email address
*/
/**
* Exercise 05
*
* Replace all the items in the array with a string value of 'deleted'
*/
@BarryAlvord
Copy link
Copy Markdown

BarryAlvord commented Sep 22, 2021

Check whether every email address in the list of users is valid(get a true or false value):

const reEmail =
    /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

var validateEmail = users.map(
    (user) => `${reEmail.test(user.email)} :${user.email} `
);
console.log(validateEmail);

Find out how many users have an IP address in a Class A network:

var A_team = users.filter((user) => user.ip_address.split(".")[0] < 128);
console.log(A_team.length);

Find the position in the array of the first user object that has a Class B IP address:

var B_team = users.findIndex(
    (user) =>
    user.ip_address.split(".")[0] >= 128 && user.ip_address.split(".")[0] < 191
);
console.log(B_team);

Find the user object that has the 'soundcloud' email address:

var findSoundCloud = users.find(
    (user) =>
    user.email.substring(
        user.email.indexOf("@") + 1,
        user.email.indexOf(".")
    ) === "soundcloud"
);
console.log(findSoundCloud);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment