Show List

JavaScript Coding Questions

  • Write a function that takes two arguments and returns their sum.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy 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.
javascript
Copy code
function isAllDigits(str) { return /^\d+$/.test(str); } console.log(isAllDigits('123')); // Output: true console.log(isAllDigits('abc')); // Output: false

    Leave a Comment


  • captcha text