Enrichvia - Business Together To Make Investments

JavaScript – Find Coding questions for interviews – Frontend developer interview questions and answers

 

“Software Developer’s life – Practice coding is always helpful either you do revision or solving new coding problems.”

Below list of questions are from my interview experiences and it will be surely helpful for your next interview !!

1. Question on setInterval in JavaScript – print even numbers from an array at intervals of 2 seconds

let arr = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

printEvenNumbers(arr) => {
let index = 0
let intervalId = setInterval(() => {
if (index >= arr.length) {
clearInterval(intervalId)
} else if (arr[index] % 2 === 0) {
console.log(arr[index])
}
index++
}, 2000)
}

printEvenNumbers(arr);

Output Expectation: 12 14 16 18 20 22

2. How would you group JSON data by a specific field name? Given the input and expected output below, Write function to convert this json Data transformation.

Input:

[
{"name": "John","age": 31,"region": "USA"},
{"name": "Jack", "age": 27,"region": "USA"},
{"name": "Carol", "age": 28,"region": "Sweden"},
{"name": "Zayn","age": 31,"region": "Denmark"},
{"name": "Mathew","age": 28,"region": "Sweden"},
{"name": "Abraham", "age": 35,"region": "Germany"},
{"name": "Daniel","age": 27,"region": "USA"},
{"name": "Zara","age": 31,"region": "Germany"}
]

Output Expectation:

{
"USA": [
{"name": "John","age": 31,"region": "USA"},
{"name": "Jack", "age": 27,"region": "USA"},
{"name": "Daniel","age": 27,"region": "USA"}
],
"Sweden": [
{"name": "Carol", "age": 28,"region": "Sweden"},
{"name": "Mathew","age": 28,"region": "Sweden"}
],
"Germany": [
{"name": "Zara","age": 31,"region": "Germany"},
{"name": "Abraham", "age": 35,"region": "Germany"}
],
"Denmark": [
{"name": "Zayn","age": 31,"region": "Denmark"}
]
}

2. Group By Field Name in Javascript:

function groupByFieldName(data: any, category: string) {

let finalResult: any = {}

for(let i=0; i< data.length ;i++){

let key = data[i][category];

if(!finalResult[key]){

let result = data.filter((item: any) => {

return item[category] == key

})

finalResult[key] = result;

}
console.log("finalResult", finalResult);
}}

 

 

3. Write a function that takes two objects and returns a new object with combined key-value pairs. If the same key exists in both objects, the value from the second object should overwrite the first.
Input:

const obj1 = { a: 1, b: 2, c: 3 };
const obj2 = { b: 2, c: 4, d: 5 };

Output: { a: 1, b: 2, c: 4, d: 5 }

2. Transform Array of Objects:

Input: [{ id: 1, name: 'A' }, { id: 2, name: 'B' }]
Output: { 1: { name: 'A' }, 2: { name: 'B' } }

3. Convert Array of Strings to Object Format:

Input: ["apple", "banana", "cherry"],

convert it into an object where each element is a key with a default value.

Output: { "apple": 0, "banana": 0, "cherry": 0 }

4. Array Mapping: Write a function to take an array of numbers and return an array of squared values.

5. Group by Property: Given an array of objects, group them by a specified property.
Input:

[
{ name: 'A', age: 20, city: 'BLR' },
{ name: 'B', age: 20, city: 'DEL' },
{ name: 'C', age: 21, city: 'BLR' }
];

Output:

{
"BLR": [{ name: 'A', age: 20 }, { name: 'C', age: 21 }],
"DEL": [{ name: 'B', age: 20 }]
}

6. Write a function to identify duplicates in an array and return them as a new array.
7. Write a function to count the frequency of each element from given array and return an object with each element and its count.
8. Write a function to find and return the longest string in an array of strings.
9. Write a function to count the number of vowels in a string.
10. Write a function to flatten a nested array.

By Enrichvia WebTeam

Jobs & Education Portal

Leave a Reply

Your email address will not be published. Required fields are marked *