Level up your JavaScript Coding Skills
If you have a considerable amount of experience with JavaScript, you are expected to solve complex coding challenges. For beginners, JavaScript coding challenges are not that big a deal. But an experienced JavaScript developer should know how to understand and solve coding challenges efficiently.
A JavaScript developer can be categorized into one of the following categories according to experience.
- Beginner
- Mid-level
- Experienced
In this article, we will discuss three coding JavaScript challenges for mid-level developers.
Extract the values of a given Property From an Array of Objects
An experienced JavaScript should have a proper working knowledge of arrays and objects because these data structures are heavily used in modern JavaScript development. By giving this coding challenge, the interviewers want to see the developer’s approach for working with arrays and objects.
The following can be the input.
let input = [
{ name: "John", age: 21, city: "New York" },
{ name: "Mike", age: 28, city: "Moscow" },
{ name: "Danny", age: 30, city: "London" },
{ name: "Lisa", age: 26, city: "Paris" },
{ name: "Sophie", age: 19, city: "Berlin" },
];
For this challenge, the value of the “name” property from every object should be extracted and stored in an array. The output should be like the following:
["John", "Mike", "Danny", "Lisa", "Sophie"];
To solve this challenge, take an empty array and then iterate through the array of objects. With every iteration, push the value of the “name” property for every object using the in-built push() function.
let input = [ { name: "John", age: 21, city: "New York" }, { name: "Mike", age: 28, city: "Moscow" }, { name: "Danny", age: 30, city: "London" }, { name: "Lisa", age: 26, city: "Paris" }, { name: "Sophie", age: 19, city: "Berlin" },
]; const extractValues = (arr, property) => { let output = []; for (let i = 0; i < arr.length; i++) { output.push(arr[i][property]); } return output;
}; let result = extractValues(input, "name");
console.log(result);
In the above code, a function is created so that the code looks more organized. Remember, an experienced developer is expected to write clean and organized code.
The above code will solve the challenge but there is one problem. An experienced JavaScript Engineer should use ES6 syntax instead of the older ES5 syntax. So let’s replace the for loop with the ES6 map() function.
const extractValues = (arr, property) => { let output = arr.map((element) => element[property]); return output;
};
So this coding challenge checks whether you know how to work with arrays and objects in JavaScript with the help of ES6 syntax.
Rotate the String k Times in the Left Direction
Along with Numbers, strings are the most commonly used data types in Javascript. An experienced JavaScript developer should know how to work with strings and should have the proper knowledge of in-built string functions.
A string along with a number will be the input.
let string = "javascript"
let n = 2
According to the above input, the given string should be rotated two times in the left direction. Following should be the output.
"vascriptja"
This coding challenge is to check your string knowledge and how you write the logic.
The simplest way to solve this challenge is by making the right use of the in-built substring() function.
function rotateLeft(string, n) { let part1 = string.substring(n, string.length); let part2 = string.substring(0, n); let output = part1 + part2; return output;
} let string = "javascript";
let n = 2; console.log(rotateLeft(string, n));
When n = 2, the first two characters – “ja” are removed and added to the end, this rotating the string to the left two times.
So first, the substring except the first two characters is extracted (part1) and then those two remaining characters (part2) are extracted as substring using the same function. Finally, both the substrings are concatenated.
Calculate the Number of Days Between Two Dates
The motive of this coding challenge is to check your level of experience with JavaScript. A beginner, usually, does not have any working experience with dates but an experienced developer must have worked with dates because working with dates is a common part of JavaScript development.
The input will consist of two dates in DD/MM/YYYY format.
let date1 = "08/10/2021";
let date2 = "09/11/2021"
To solve this challenge, you must know which in-built date function to use.
const findDays = (date1, date2) => { let d1 = new Date(date1); let d2 = new Date(date2); let difference = d2.getTime() - d1.getTime(); let output = difference / (1000 * 3600 * 24); return Math.floor(output);
}; let date1 = "11/01/2021";
let date2 = "11/03/2021"; console.log(findDays(date1, date2));
As mentioned, knowledge of date functions is required to solve this challenge. Here, first, the date is converted into the right format, and then, the getTime() method is used to find the time difference between the dates.
Another motive of this challenge is to check your calculation solving technique by asking you to find the days using the difference in the time.
Wrapping it Up
The coding challenges for mid-level experienced JavaScript developers usually focus on the knowledge of JavaScript that could be attained only through experience. For example, the date coding challenge. Another important area is the knowledge of ES6. Moreover, the clarity of concepts such as arrays, objects, and strings is very important for mid-level JavaScript developers. Last but not the least, the style of coding is also considered for an experienced JavaScript developer.
Tags
Top 3 Coding Challenges for Mid-level JavaScript developers
Source: Pinay Tube PH
0 Comments