Q1: Find the Intersection of Two Arrays.
Write a function that returns the common elements between two arrays.
Ans:
function arrayIntersection(arr1, arr2) {
return arr1.filter(item => arr2.includes(item));
}
console.log(arrayIntersection([1, 2, 3, 4], [3, 4, 5, 6]));
// Output: [3, 4]
๐ง Tip: Use Set for better performance with large datasets.
Q2: Capitalize the First Letter of Each Word.
Write a function that takes a sentence and capitalizes the first letter of each word.
Ans:
function capitalizeWords(sentence) {
return sentence
.split(" ")
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
}
console.log(capitalizeWords("hello world from javascript"));
// Output: "Hello World From Javascript"
Q3: Count the Frequency of Each Element in an Array
Write a function that counts how many times each element appears in an array.
Ans:
function countFrequency(arr) {
return arr.reduce((acc, item) => {
acc[item] = (acc[item] || 0) + 1;
return acc;
}, {});
}
console.log(countFrequency(["apple", "banana", "apple", "orange", "banana", "apple"]));
// Output: { apple: 3, banana: 2, orange: 1 }
Q4: Check if Two Strings Are Anagrams
Write a function to check whether two strings are anagrams of each other.
Ans:
Anagrams are words or phrases that can be rearranged to make an entirely different word or phrase.
function isAnagram(str1, str2) {
const normalize = str => str.toLowerCase().split("").sort().join("");
return normalize(str1) === normalize(str2);
}
console.log(isAnagram("listen", "silent")); // true
console.log(isAnagram("hello", "world")); // false
๐ก Explanation:
We normalize both strings by converting them to lowercase, sorting, and comparing.
If both sorted versions match โ theyโre anagrams!
Q5: Chunk an Array
Write a function that splits an array into chunks of a specified size.
Ans:
function chunkArray(arr, size) {
let result = [];
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
}
console.log(chunkArray([1, 2, 3, 4, 5, 6, 7], 3));
// Output: [[1, 2, 3], [4, 5, 6], [7]]
๐ก Explanation:
This is the core part of the loop that allows the function to chunk the array efficiently. Letโs go step by step ๐
for (let i = 0; i < arr.length; i += size)
๐งฉ 1. Normal for-loop structure
A typical for loop looks like this:
for (initialization; condition; increment) {
// code to execute
}
Here:
- initialization โ sets up a variable (like let i = 0)
- condition โ loop continues while this is true
- increment โ runs after each loop iteration (to move to the next step)
๐ง 2. In this code:
for (let i = 0; i < arr.length; i += size)
- let i = 0 โ start from index 0
- i < arr.length โ keep looping until i reaches the end of the array
- i += size โ instead of increasing i by 1, we increase it by size That means each time, the loop jumps forward by size elements.
๐ก 3. Example to visualize
Suppose:
arr = [1, 2, 3, 4, 5, 6, 7]
size = 3
Hereโs how the loop runs:
Iteration |
i Value |
Slice (arr.slice(i, i + size) ) |
Result |
---|---|---|---|
1 | 0 | arr.slice(0, 3) โ [1, 2, 3]
|
result = [[1,2,3]]
|
2 | 3 | arr.slice(3, 6) โ [4, 5, 6]
|
result = [[1,2,3],[4,5,6]]
|
3 | 6 | arr.slice(6, 9) โ [7]
|
result = [[1,2,3],[4,5,6],[7]]
|
After that, i = 9, which is greater than arr.length (7), so the loop stops.
โ Final Thoughts
These questions are simple yet powerful for testing problem-solving and array manipulation in JavaScript.
If you havenโt already, check out my previous parts:
๐ Part 1
๐ Part 2
๐ Part 3
๐ Part 4
๐ Part 5
๐ Part 6
๐ Part 7
Top comments (0)