11. Create a function to sort an array of objects by a given property.
Required Input:
[ { name: "John", age: 30 }, { name: "Alice", age: 25 }, { name: "Bob", age: 35 } ]
Expected Output:
[ { name: 'Alice', age: 25 },
{ name: 'John', age: 30 },
{ name: 'Bob', age: 35 } ]
Code In Javascript
// Sort an array of objects by a specified property
function sortByProperty(arr, property) {
}
let data = [
{ name: "John", age: 30 },
{ name: "Alice", age: 25 },
{ name: "Bob", age: 35 }
];
console.log(sortByProperty(data, "age"));
Run Code?
Click Run Button to view compiled output
12. Write a function to calculate the sum of all prime numbers up to a given number.
Required Input:
n = 10
Expected Output:
17
Code In Javascript
// Sum all prime numbers up to n
function sumPrimes(n) {
}
console.log(sumPrimes(10));
Run Code?
Click Run Button to view compiled output
13. Implement a function that finds the intersection of two arrays.
Required Input:
[1, 2, 3, 4], [3, 4, 5, 6]
Expected Output:
[ 3, 4 ]
Code In Javascript
// Find intersection of two arrays
function intersection(arr1, arr2) {
}
console.log(intersection([1, 2, 3, 4], [3, 4, 5, 6]));
Run Code?
Click Run Button to view compiled output
14. Write a function to capitalize the first and last letters of each word in a string.
Required Input:
"hello world"
Expected Output:
HellO WorlD
Code In Javascript
// Capitalize the first and last letters of each word
function capitalizeFirstLast(str) {
}
console.log(capitalizeFirstLast("hello world"));
Run Code?
Click Run Button to view compiled output
15. Create a function to find all pairs in an array that add up to a specific target.
Required Input:
array = [1, 2, 3, 4, 5], target = 6
Expected Output:
[ [ 1, 5 ], [ 2, 4 ] ]
Code In Javascript
// Find all pairs that add up to the target
function findPairs(arr, target) {
}
console.log(findPairs([1, 2, 3, 4, 5], 6));
Run Code?
Click Run Button to view compiled output
16. Write a program to check if a string is a pangram (contains every letter of the alphabet).
Required Input:
"The quick brown fox jumps over the lazy dog"
Expected Output:
true
Code In Javascript
// Check if a string is a pangram
function isPangram(str) {
}
console.log(isPangram("The quick brown fox jumps over the lazy dog"));
Run Code?
Click Run Button to view compiled output
17. Create a function to rotate elements in an array to the right by k positions.
Required Input:
array = [1, 2, 3, 4, 5], k = 2
Expected Output:
[ 4, 5, 1, 2, 3 ]
Code In Javascript
// Rotate array elements to the right by k positions
function rotateArray(arr, k) {
}
console.log(rotateArray([1, 2, 3, 4, 5], 2));
Run Code?
Click Run Button to view compiled output
18. Write a function that returns true if a given number is a palindrome.
Required Input:
num = 121
Expected Output:
true
Code In Javascript
// Check if a number is a palindrome
function isPalindrome(num) {
}
console.log(isPalindrome(121));
Run Code?
Click Run Button to view compiled output
19. Create a function to remove all occurrences of a specific element from an array.
Required Input:
array = [1, 2, 3, 2, 4, 2], element = 2
Expected Output:
[ 1, 3, 4 ]
Code In Javascript
// Remove all occurrences of a specific element
function removeElement(arr, element) {
}
console.log(removeElement([1, 2, 3, 2, 4, 2], 2));
Run Code?
Click Run Button to view compiled output
20. Write a program to check if a given date is a weekend.
Required Input:
"2023-11-04"
Expected Output:
true
Code In Javascript
// Check if a date is a weekend
function isWeekend(dateStr) {
}
console.log(isWeekend("2023-11-04"));
Run Code?
Click Run Button to view compiled output