You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write a function that accepts an array of number, and returns the greatest of them:
functionmax(numbers){// find a strategy to determine the biggest entry of the array// hints:// declaring a variable with let// and looping over the numbers// may be helpful!}// test casesconsole.log(max([1,2,3]));// logs 3console.log(max([3,1,2]));// logs 3console.log(max([-3,1,2]));// logs 2console.log(max([]));// logs undefined
Min of variadic arguments
Write a function that accepts multiple arguments, and returns the lesser of them:
functionmin(...numbers){// see above}// test casesconsole.log(min(1,2,3));// logs 1console.log(min(3,1,]));// logs 1console.log(min(-3,1,2));// logs -3console.log(min());// logs undefined
Generate the email for a user
New friends are joining your company, and you have to provide them with a username.
Write a function that accepts two parameters: an object with firstName and lastName keys, and a string that contains the website of your company.
It should return {firstName}.{lastName}@{domain}.com (see example below).
If no lastName is provided in the object, use the first name only.
functiongetEmail(user,website){// observe how the email is all in lowercase// observe how the www should not be part of the email// useful string tools:// SEE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase// SEE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace}console.log(getEmail({firstName: 'Jane',lastName: 'Doe'},'www.flamingos.com'));// logs 'jane.doe@flamingos.com'console.log(getEmail({firstName: 'John',lastName: 'Doe'},'www.acme.net'));// logs 'john.doe@acme.net'console.log(getEmail({firstName: 'John'},'www.flamingos.com'));// logs 'john@flamingos.com'
While a human eye can tell the biggest number in a row with a glimpse, computers are almost blind and prefer another approach: they start from the beginning of the array, and check if the current number is bigger than any they have seen so far:
functionmax(numbers){// let's set the first number as the potential maxletmaxNumber=numbers[0];// we can safely start from 1, why?for(leti=1;i<numbers.length;i++){// if the current number in the loop is greater than the current max...if(numbers[i]>maxNumber){// ...set it as the new max!maxNumber=numbers[i];}}// don't forget to return your result!returnmaxNumber;}// test casesconsole.log(max([1,2,3]));// logs 3console.log(max([3,1,2]));// logs 3console.log(max([-3,1,2]));// logs 2console.log(max([]));// logs undefined
You can use Array.prototype.forEach if you prefer:
functionmax(numbers){letmaxNumber=numbers[0];// observe how this way you cannot skip the first iterationnumbers.forEach((n)=>{if(n>maxNumber){maxNumber=n;}});returnmaxNumber;}
Min of variadic arguments
There are two little plot twists here - we have to find the min and not the max, and the function signature differs: instead of a single argument of type array, the function accepts a variable number of arguments:
// we use the Rest parameters syntax// SEE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parametersfunctionmin(...numbers){letminNumber=numbers[0];for(leti=1;i<numbers.length;i++){if(numbers[i]<minNumber){minNumber=numbers[i];}}returnminNumber;}// test casesconsole.log(min(1,2,3));// logs 1console.log(min(3,1,]));// logs 1console.log(min(-3,1,2));// logs -3console.log(min());// logs undefined
Generate the email for a user
Here the function signature gets a bit more interesting, because we expect an object and a string, and inside the function we'll have to access two properties of the object (firstName and lastName).
This problem can be split in two sub-problems:
formatting the part before the @, i.e. the username;
formatting the part after the @, i.e. the domain.
Let's start with the first
functiongetEmail(user,website){// let's destructure the props we need so we have to type less annoying dotsconst{ firstName, lastName }=user;constuserName=`${firstName}.${lastName}`;// ...}
Let's stop here for a second and let's check if this is working. You can do so by calling the function and logging what the function returns so far:
Fixed! Now we can move forward to the domain part, or cover the case when the last name is not provided. Let's finish the username part first:
functiongetEmail(user,website){const{ firstName, lastName }=user;// let's declare the variable with let, so we can decide what to assign in the following ifletuserName;if(lastName){userName=`${firstName.toLowerCase()}.${lastName.toLowerCase()}`;}else{userName=firstName.toLowerCase();}returnuserName;}console.log(getEmail({firstName: 'Jane'}));// logs 'jane'console.log(getEmail({firstName: 'Jane',lastName: 'Doe'}));// logs 'jane.doe'
Great! Observe how we have to check TWO scenarios now, because we introduced an if in our code and we have to make sure both branches work as intended. This is a very important concept in unit testing - here we are checking things in an intuitive way and it's fine!
Now there are 2904809248 ways one could organise this if decision...but it's not important! As long as the WHOLE function satisfies its contract with the caller, don't waste time in optimising such small things. It's readable, it does its job, and it's not like you are generating million of emails per second where a nanosecond will matter. An alternative with a ternary operator because you know you want it:
// I can live with itconstuserName=lastName
? `${firstName.toLowerCase()}.${lastName.toLowerCase()}`
: firstName.toLowerCase();
Now for the domain part: the challenge is to remove the www. part from the passed website. For simplicity's sake we will assume that all the test cases will start with www, i.e. no http, no strings that are different than www.{something}.{somethingElse}.
Now this function is not huge, but it's still clearly doing to different things inside it. It would be great to move these two different things in two different functions:
Observe how after this process the behaviour of the function stays the same, but the code is more organised. This process is called refactoring, and usually you don't have time to do that because your project manager wants you to add more features instead.
For instance, if you want to remove the http:// part from the website now, you can do so by working in the getDomain function only:
functiongetDomain(website){// important: there are better js functions that parse URLs// and give you the protocol, the domain, the query string, etc...// this is just an example!returnwebsite.replace('http://','').replace('www.','');}