21. Create a function that returns the square of each number in an array.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
[ 1, 4, 9, 16, 25 ]
Code In Javascript
// Square each number in an array
function squareArray(numbers) {
return numbers;
}
console.log(squareArray([1, 2, 3, 4, 5]));
Run Code?
Click Run Button to view compiled output
22. Write a function to capitalize the first letter of each word in a string.
Required Input:
"hello world"
Expected Output:
Hello World
Code In Javascript
// Capitalize the first letter of each word
function capitalizeWords(str) {
return str;
}
console.log(capitalizeWords("hello world"));
Run Code?
Click Run Button to view compiled output
23. Create a program to merge two arrays and remove duplicates.
Required Input:
[1, 2, 3], [3, 4, 5]
Expected Output:
[ 1, 2, 3, 4, 5 ]
Code In Javascript
// Merge arrays and remove duplicates
let array1 = [1, 2, 3];
let array2 = [3, 4, 5];
let mergedArray = [];
console.log(mergedArray);
Run Code?
Click Run Button to view compiled output
24. Write a program to find the second largest number in an array.
Required Input:
[10, 5, 8, 12, 7]
Expected Output:
10
Code In Javascript
// Find the second largest number in an array
let numbers = [10, 5, 8, 12, 7];
let uniqueNumbers = ;
console.log(uniqueNumbers[]);
Run Code?
Click Run Button to view compiled output
25. Create a function that checks if all elements in an array are equal.
Required Input:
[5, 5, 5, 5]
Expected Output:
true
Code In Javascript
// Check if all elements in an array are equal
function allEqual(array) {
return array;
}
console.log(allEqual([5, 5, 5, 5]));
Run Code?
Click Run Button to view compiled output
26. Write a program to generate a random number between 1 and 100.
Required Input:
None
Expected Output:
64
Code In Javascript
// Generate a random number between 1 and 100
let randomNumber = ;
console.log(randomNumber);
Run Code?
Click Run Button to view compiled output
27. Create a function that takes an array and returns the largest and smallest elements.
Required Input:
[3, 7, 1, 9, 4]
Expected Output:
{ min: 1, max: 9 }
Code In Javascript
// Find the largest and smallest elements in an array
function findMinMax(arr) {
return { min, max };
}
console.log(findMinMax([3, 7, 1, 9, 4]));
Run Code?
Click Run Button to view compiled output
28. Write a program to create a countdown from 10 to 1.
Required Input:
None
Expected Output:
10
9
8
7
6
5
4
3
2
1
Code In Javascript
// Countdown from 10 to 1
for () {
console.log(i);
}
Run Code?
Click Run Button to view compiled output
29. Create a function that removes all falsy values (false, 0, "", null, undefined, NaN) from an array.
Required Input:
[0, 1, false, 2, "", 3, null, undefined, NaN]
Expected Output:
[ 1, 2, 3 ]
Code In Javascript
// Remove all falsy values from an array
function removeFalsy(arr) {
return ;
}
console.log();
Run Code?
Click Run Button to view compiled output
30. Write a program that calculates the sum of squares of numbers from 1 to n.
Required Input:
n = 3
Expected Output:
14
Code In Javascript
// Calculate the sum of squares from 1 to n
function sumOfSquares(n) {
}
console.log(sumOfSquares(3));
Run Code?
Click Run Button to view compiled output