JSchallenger にチャレンジ - Fundamentals / Arrays
Notion にまとめていたものを少しずつ移行中。後ほど解説も追加します。
Fundamentals 編
JSchallenger にチャレンジ - Fundamentals / Basics
JSchallenger にチャレンジ - Fundamentals / Arrays
JSchallenger にチャレンジ - Fundamentals / Objects
JSchallenger にチャレンジ - Fundamentals / Dates
JSchallenger にチャレンジ - Fundamentals / Sets
DOM 編
JSchallenger にチャレンジ - DOM / DOM selector methods
JSchallenger にチャレンジ - DOM / Events and user interactions
JSchallenger にチャレンジ - DOM / DOM manipulation
JSchallenger にチャレンジ - DOM / DOM fundamentals
JSchallenger にチャレンジ - DOM / Recursive functions
Get nth element of array
Write a function that takes an array (a) and a value (n) as argument
Return the nth element of 'a'
回答
function myFunction(a, n) {
return a[n-1];
}
Remove first n elements of an array
Write a function that takes an array (a) as argument
Remove the first 3 elements of 'a'
Return the result
回答
function myFunction(a) {
return a.slice(3);
}
関連項目
Get last n elements of an array
Write a function that takes an array (a) as argument
Extract the last 3 elements of a
Return the resulting array
回答
function myFunction(a) {
return a.slice(-3);
}
関連項目
Get first n elements of an array
Write a function that takes an array (a) as argument
Extract the first 3 elements of a
Return the resulting array
回答
function myFunction(a) {
return a.slice(0,3)
}
関連項目
Return last n array elements
Write a function that takes an array (a) and a number (n) as arguments
It should return the last n elements of a
回答
function myFunction(a, n) {
return a.slice(-n)
}
関連項目
Remove a specific array element
Write a function that takes an array (a) and a value (b) as argument
The function should clean a from all occurrences of b
Return the filtered array
回答
function myFunction(a, b){
return a.filter(cur => cur !== b)
}
関連項目
Count number of elements in JavaScript array
Write a function that takes an array (a) as argument
Return the number of elements in a
回答
function myFunction(a) {
return a.length;
}
関連項目
Count number of negative values in array
Write a function that takes an array of numbers as argument
Return the number of negative values in the array
回答
function myFunction(a) {
return a.filter(cur => cur < 0).length
}
関連項目
Sort an array of numbers in descending order
Write a function that takes an array of numbers as argument
It should return an array with the numbers sorted in descending order
回答
function myFunction(arr) {
return arr.sort().reverse()
}
関連項目
Sort an array of strings alphabetically
Write a function that takes an array of strings as argument
Sort the array elements alphabetically
Return the result
回答
function myFunction(arr) {
return arr.sort()
}
関連項目
Return the average of an array of numbers
Write a function that takes an array of numbers as argument
It should return the average of the numbers
回答
function myFunction(arr) {
return arr.reduce((acc, cur) => acc + cur) / arr.length;
}
関連項目
Return the longest string from an array of strings
Write a function that takes an array of strings as argument
Return the longest string
回答
function myFunction(arr) {
return arr.reduce((prev, current) =>
prev.length > current.length ? prev : current)
}
関連項目
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
}
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()
}
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);
}
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)),
);
}
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,
);
}
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)
}
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;
}
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)
}
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)
}
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)
}