JavaScript Function Question
JavaScript Function Question
function addNumbers(a, b) {
return a + b;
}const result = addNumbers(5, 10);
console.log(result);
function calculateAverage(numbers) {
if (numbers.length === 0) {
return 0; // Return 0 if the array is empty
}
return maxValue;
}console.log(findMaxValue([10, 5, 8, 12, 7]));
console.log(findMaxValue([-3, -1, -5, -2]));
console.log(findMaxValue([]));
7. Write a function that generates a random number between a given minimum and maximum.
function getRandomNumber(min, max) {
if (min > max) {
[min, max] = [max, min];
}
9. Write a function that capitalizes the first letter of each word in a sentence.
function capitalizeWords(sentence) {
11. Write a function that counts the number of occurrences of a specific element in an array.
function countOccurrences(arr, element) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === element) {
count++;
}
}
return count;
}const myArray = [1, 2, 3, 2, 4, 2, 5, 2];
console.log(countOccurrences(myArray, 2));
console.log(countOccurrences(myArray, 3));
console.log(countOccurrences(myArray, 6));
13. Write a function that merges two arrays and returns the sorted result.
function mergeSortedArrays(arr1, arr2) {
return (arr1.concat(arr2)).sort((a, b) => a - b);
}console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6]));
console.log(mergeSortedArrays([1, 2, 3], [4, 5, 6]));
14. Create a function that converts a number to its Roman numeral representation.
function convertToRoman(num) {
const romanSymbols = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1],
];
return result;
}console.log(convertToRoman(3));
console.log(convertToRoman(4));
console.log(convertToRoman(9));
console.log(convertToRoman(58));
console.log(convertToRoman(1994));
15. Write a function that finds the second smallest element in an array of numbers.
function findSecondSmallest(arr) {
let smallest = Infinity;
let secondSmallest = Infinity;
return secondSmallest;
}console.log(findSecondSmallest([5, 2, 3, 1, 4]));
console.log(findSecondSmallest([1, 1, 1, 1, 1]));
16. Create a function that calculates the area of a circle given its radius.
function calculateCircleArea(radius) {
return area;
}console.log(calculateCircleArea(5));
console.log(calculateCircleArea(-5));
17. Write a function that truncates a string if it exceeds a specified length and appends "..." to the end.
function truncateString(str, maxLength) {
19. Write a function that removes all falsy values (false, null, 0, "", undefined, and NaN) from an array.
function removeNullish(arr) {
return arr.filter(item => item !== false && item !== null && item !== 0
&& item !== '' && item !== undefined && !isNaN(item));
}console.log(removeNullish([1, 0, 2, '', 3, null, undefined, NaN]));
console.log(removeNullish([false, true, 42]));
20. Create a function that generates a new array with unique values from a given array.
function getUniqueValues(arr) {
const uniqueSet = new Set(arr);
return Array.from(uniqueSet);
}console.log(getUniqueValues([1, 2, 3, 2, 4, 1, 5]));
console.log(getUniqueValues(['apple', 'banana', 'cherry', 'banana']));
console.log(getUniqueValues([false, true, false, 0, 1, 0]));