Basic Javascript Examples — Part 2
Let’s start with an example “Generate a random email address”
Implement a JavaScript function called getRandomEmail
that takes a domain name as input and generates a random email address. If the function is called multiple times with the same domain name, it should generate a different email address every time. For example: If the input is the domain name ‘outlook.com’, the expected output needs to contain a random string and the domain name provided.
Input: outlook.com Output: hfj56ms@outlook.com
By using console.log(random); inside the function, you can see in the console what the random is.
Now, let’s look at the way of writing functions.
This is the modern way of writing functions in JavaScript.
Here, we use callbackFunction and setTimeout method.
The setTimeout()
method allows you to execute a piece of code after a certain amount of time has passed. You can think of the method as a way to set a timer to run JavaScript code at a certain time.
This is the end of the Part 2. Have a nice testing day!
We often think that great achievements require great actions. However, it is surprising the difference a small improvement can make over time. If you improve by 1 percent every day for 1 year, you will be 37 times better by the end of the year. So, keep working!