Show List
JavaScript Coding Questions
- Write a function that takes two arguments and returns their sum.
javascriptCopy code
function sum(a, b) {
return a + b;
}
console.log(sum(1, 2)); // Output: 3
- Write a function that takes an array of numbers and returns their sum.
javascriptCopy code
function sum(numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum([1, 2, 3, 4])); // Output: 10
- Write a function that takes an array of numbers and returns the average.
javascriptCopy code
function average(numbers) {
return numbers.reduce((total, num) => total + num, 0) / numbers.length;
}
console.log(average([1, 2, 3, 4])); // Output: 2.5
- Write a function that takes an array of strings and returns the longest string.
javascriptCopy code
function longestString(strings) {
return strings.reduce((longest, str) => str.length > longest.length ? str : longest, '');
}
console.log(longestString(['apple', 'banana', 'cherry'])); // Output: 'banana'
- Write a function that takes an array of numbers and returns a new array with the numbers sorted in ascending order.
javascriptCopy code
function sortNumbers(numbers) {
return numbers.sort((a, b) => a - b);
}
console.log(sortNumbers([3, 1, 4, 2])); // Output: [1, 2, 3, 4]
- Write a function that takes an array of strings and returns a new array with the strings sorted in alphabetical order.
javascriptCopy code
function sortStrings(strings) {
return strings.sort();
}
console.log(sortStrings(['banana', 'apple', 'cherry'])); // Output: ['apple', 'banana', 'cherry']
- Write a function that takes a string and returns the reverse of the string.
javascriptCopy code
function reverseString(str) {
return str.split('').reverse().join('');
}
console.log(reverseString('hello')); // Output: 'olleh'
- Write a function that takes a string and returns the number of vowels in the string.
javascriptCopy code
function countVowels(str) {
return str.match(/[aeiou]/gi).length;
}
console.log(countVowels('hello world')); // Output: 3
- Write a function that takes a string and returns true if the string is a palindrome, false otherwise.
javascriptCopy code
function isPalindrome(str) {
return str === str.split('').reverse().join('');
}
console.log(isPalindrome('racecar')); // Output: true
console.log(isPalindrome('hello')); // Output: false
- Write a function that takes an array of numbers and returns the largest number.
javascriptCopy code
function largestNumber(numbers) {
return Math.max(...numbers);
}
console.log(largestNumber([1, 2, 3, 4])); // Output: 4
- Write a function that takes a number and returns true if the number is even, false otherwise.
javascriptCopy code
function isEven(num) {
return num % 2 === 0;
}
console.log(isEven(2)); // Output: true
console.log(isEven(3)); // Output: false
- Write a function that takes an array of numbers and returns a new array with only the even numbers.
javascriptCopy code
function filterEvenNumbers(numbers) {
return numbers.filter(num => num % 2 === 0);
}
console.log(filterEvenNumbers([1, 2, 3, 4])); // Output: [2, 4]
- Write a function that takes an array of strings and returns a new array with only the strings that have a length of 3.
javascriptCopy code
function filterStringsByLength(strings) {
return strings.filter(str => str.length === 3);
}
console.log(filterStringsByLength(['apple', 'banana', 'cherry'])); // Output: ['apple']
- Write a function that takes an array of numbers and returns the sum of the squares of those numbers.
javascriptCopy code
function sumOfSquares(numbers) {
return numbers.reduce((total, num) => total + num ** 2, 0);
}
console.log(sumOfSquares([1, 2, 3])); // Output: 14
- Write a function that takes a string and returns the number of words in the string.
javascriptCopy code
function countWords(str) {
return str.split(' ').length;
}
console.log(countWords('hello world')); // Output: 2
- Write a function that takes an array of numbers and returns the product of those numbers.
javascriptCopy code
function product(numbers) {
return numbers.reduce((total, num) => total * num, 1);
}
console.log(product([1, 2, 3, 4])); // Output: 24
- Write a function that takes an array of numbers and returns the sum of all the negative numbers in the array.
javascriptCopy code
function sumOfNegatives(numbers) {
return numbers.filter(num => num < 0).reduce((total, num) => total + num, 0);
}
console.log(sumOfNegatives([1, -2, 3, -4])); // Output: -6
- Write a function that takes an array of strings and returns a new array with only the strings that start with a vowel.
javascriptCopy code
function filterStringsByVowel(strings) {
return strings.filter(str => /^[aeiou]/i.test(str));
}
console.log(filterStringsByVowel(['apple', 'banana', 'cherry'])); // Output: ['apple']
- Write a function that takes a string and returns a new string with the first letter of each word capitalized.
javascriptCopy code
function capitalizeWords(str) {
return str.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
}
console.log(capitalizeWords('hello world')); // Output: 'Hello World'
- Write a function that takes an array of numbers and returns a new array with only the unique numbers.
javascriptCopy code
function getUniqueNumbers(numbers) {
return [...new Set(numbers)];
}
console.log(getUniqueNumbers([1, 2, 2, 3, 3, 4])); // Output: [1, 2, 3, 4]
- Write a function that takes a string and returns true if the string contains only digits, false otherwise.
javascriptCopy code
function isAllDigits(str) {
return /^\d+$/.test(str);
}
console.log(isAllDigits('123')); // Output: true
console.log(isAllDigits('abc')); // Output: false
Leave a Comment