Skip to content

Latest commit

 

History

History
53 lines (31 loc) · 2.8 KB

14-array-exercise.md

File metadata and controls

53 lines (31 loc) · 2.8 KB

Array Exercise

  1. Create an array called fruits that contains the following elements: "apple", "banana", "orange". Now check if "orange" is in the fruits array.

  2. Given an array of numbers, write a function that returns the sum of all the even numbers in the array.

  3. Given two arrays of numbers, write a function that returns a new array that contains only the unique elements from both arrays.

  4. Given an array of strings, write a function that returns the longest string in the array.

  5. Write a function that takes an array of numbers and returns the largest number in the array.

  6. Write a function that takes an array of numbers and returns a new array that only contains the even numbers from the original array.

  7. Write a function that takes an array of strings and returns a new array that only contains strings with more than 5 characters.

  8. Write a function that takes two arrays of numbers and returns a new array that contains the intersection of the two arrays (i.e. only the numbers that appear in both arrays).

  9. Write a function that takes an array of numbers and returns a new array where each element is the square of the original element

  10. Write a function that takes an array of numbers and returns the average of all the numbers in the array.

  11. Write a function that takes an array of numbers and returns a new array that only contains numbers that are greater than 5. Use filter function

  12. Write a function that takes an array of numbers and returns a new array where each element is the original element plus 1. use map function

  13. Write a function that takes an array of numbers and returns a new array that contains only the unique numbers using reduce.

  14. Write a function that takes an array of strings and returns the total number of characters in all the strings using reduce.

  15. Write a function that takes an array of strings and sorts them by their length in ascending order.

// Example usage
let strings = ["apple", "banana", "cherry", "date"];
console.log(sortStringsByLength(strings)); // Output: ["date", "apple", "cherry", "banana"]
  1. Write a function that takes an array of numbers and returns the second highest number.

  2. Write a function that takes an array of numbers and returns a new array with all the unique numbers.

  3. Write a function that takes an array of strings and returns a new array with only the strings that contain the letter "a".

  4. Write a function that takes an array of numbers and returns a new array with the numbers sorted in ascending order.

  5. Write a function that takes an array of strings and flattens each string into an array of characters, then flattens the result into a single array.

// Example usage
let arr = ['hello', 'world'];
console.log(flattenStrings(arr)); // Output: ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']