Open23

JSchallenger にチャレンジ - Fundamentals / Arrays

cyamycyamy

Array - Check if all array elements are equal

Write a function that takes an array as argument
It should return true if all elements in the array are equal
It should return false otherwise

回答

function myFunction(arr) {
  return Boolean(
    arr.reduce((acc, cur) => (cur === acc ? cur : false)),
  );
}

作者回答

function myFunction( arr ) {
  return new Set(arr).size === 1
}
cyamycyamy

Array - Merge an arbitrary number of arrays

Write a function that takes arguments an arbitrary number of arrays
It should return an array containing the values of all arrays

function myFunction(...arrays) {
  return arrays.flat()
}
cyamycyamy

Array - Sort array by object property

Write a function that takes an array of objects as argument
Sort the array by property b in ascending order
Return the sorted array

function myFunction(arr) {
  return arr.sort((a ,b) => a.b - b.b);
}
cyamycyamy

Array - Merge two arrays with duplicate values

Write a function that takes two arrays as arguments
Merge both arrays and remove duplicate values
Sort the merge result in ascending order
Return the resulting array

回答

function myFunction(a, b) {
  return Array.from(
    new Set(a.concat(b).sort((a, b) => a - b)),
  );
}
cyamycyamy

Array - Sum up all array elements with values greater than

Write a function that takes an array (a) and a number (b) as arguments
Sum up all array elements with a value greater than b
Return the sum

回答

function myFunction(a, b) {
  return a.reduce(
    (prev, cur) => (cur > b ? prev + cur : prev),
    0,
  );
}

cyamycyamy

Array - Create a range of numbers

Write a function that takes two numbers (min and max) as arguments
Return an array of numbers in the range min to max

回答

function myFunction(min, max) {
  const array = [...new Array(max - min + 1)];
  return array.map((_, index) => index + min)
}
cyamycyamy

Array - Group array of strings by first letter

Write a function that takes an array of strings as argument
Group those strings by their first letter
Return an object that contains properties with keys representing first letters
The values should be arrays of strings containing only the corresponding strings
For example, the array ['Alf', 'Alice', 'Ben'] should be transformed to
{ a: ['Alf', 'Alice'], b: ['Ben']}

回答

function myFunction(arr) {
  const result =  arr.reduce((prev, current) => {
    const initial = current.split('')[0].toLowerCase();
    return { 
      ...prev, 
      [initial]: [...(prev[initial] ?? []), current] 
    };
  }, {});
  return result;
}
cyamycyamy

Array - Define an array with conditional elements

Write a function that takes an array with arbitrary elements and a number as arguments
Return a new array, the first element should be either the given number itself
or zero if the number is smaller than 6
The other elements should be the elements of the original array
Try not to mutate the original array

回答

function myFunction(arr, num) {
  return num >= 6 ? [num].concat(arr) : [0].concat(arr)
}
cyamycyamy

Array - Get every nth element of array

Write a function that takes an array (a) and a value (n) as arguments
Save every nth element in a new array
Return the new array

回答

function myFunction(a, n) 
  return a.filter((_, i) => (i+1) % n === 0)
}
cyamycyamy

Calculate the sum of an array of numbers

Write a function that takes an array of numbers as argument
It should return the sum of the numbers

回答

function myFunction(a) 
  a.reduce((acc, cur) => acc + cur)
}