21. Implement a function to count the number of words in a string.
Required Input:
"JavaScript is fun"
Expected Output:
3
Code In Javascript
// Count the number of words in a string
function countWords(str) {
}
console.log(countWords("JavaScript is fun"));
Run Code?
Click Run Button to view compiled output
22. Write a function to calculate the factorial of a number recursively.
Required Input:
n = 5
Expected Output:
120
Code In Javascript
// Calculate factorial of a number recursively
function factorial(n) {
}
console.log(factorial(5));
Run Code?
Click Run Button to view compiled output
23. Create a function to merge two sorted arrays into a single sorted array.
Required Input:
[1, 3, 5], [2, 4, 6]
Expected Output:
[ 1, 2, 3, 4, 5, 6 ]
Code In Javascript
// Merge two sorted arrays into a single sorted array
function mergeSortedArrays(arr1, arr2) {
}
console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6]));
Run Code?
Click Run Button to view compiled output
24. Write a function that returns the sum of all digits in a given integer.
Required Input:
num = 1234
Expected Output:
10
Code In Javascript
// Calculate the sum of all digits in an integer
function sumOfDigits(num) {
}
console.log(sumOfDigits(1234));
Run Code?
Click Run Button to view compiled output
25. Create a program to generate all possible substrings of a given string.
Required Input:
"abc"
Expected Output:
[ 'a', 'ab', 'abc', 'b', 'bc', 'c' ]
Code In Javascript
// Generate all possible substrings of a string
function getSubstrings(str) {
}
console.log(getSubstrings("abc"));
Run Code?
Click Run Button to view compiled output
26. Write a function that returns the most frequently occurring element in an array.
Required Input:
[1, 2, 2, 3, 3, 3, 4]
Expected Output:
3
Code In Javascript
// Find the most frequently occurring element in an array
function mostFrequent(arr) {
}
console.log(mostFrequent([1, 2, 2, 3, 3, 3, 4]));
Run Code?
Click Run Button to view compiled output
27. Create a function to reverse the words in a sentence while preserving their order.
Required Input:
"Hello World"
Expected Output:
olleH dlroW
Code In Javascript
// Reverse each word in a sentence
function reverseWords(sentence) {
}
console.log(reverseWords("Hello World"));
Run Code?
Click Run Button to view compiled output
28. Write a program to find the symmetric difference between two arrays (elements unique to each array).
Required Input:
[1, 2, 3], [2, 3, 4]
Expected Output:
[ 1, 4 ]
Code In Javascript
// Find the symmetric difference between two arrays
function symmetricDifference(arr1, arr2) {
}
console.log(symmetricDifference([1, 2, 3], [2, 3, 4]));
Run Code?
Click Run Button to view compiled output
29. Implement a function that finds the maximum sum of a subarray in a given array.
Required Input:
[1, -2, 3, 4, -1, 2, 1, -5, 4]
Expected Output:
9
Code In Javascript
// Find the maximum sum of a subarray (Kadane's algorithm)
function maxSubarraySum(arr) {
}
console.log(maxSubarraySum([1, -2, 3, 4, -1, 2, 1, -5, 4]));
Run Code?
Click Run Button to view compiled output
30. Write a function to calculate the sum of values in an array of objects based on a specific key.
Required Input:
[ { name: "Alice", value: 10 }, { name: "Bob", value: 15 }, { name: "Charlie", value: 20 } ]
key = "value"
Expected Output:
45
Code In Javascript
// Sum values in an array of objects based on a specified key
function sumByKey(arr, key) {
}
let data = [
{ name: "Alice", value: 10 },
{ name: "Bob", value: 15 },
{ name: "Charlie", value: 20 }
];
console.log(sumByKey(data, "value"));
Run Code?
Click Run Button to view compiled output