1 const intersection = (a, b) => {
2 const bSet = new Set(b);
3 return a?.filter(value => bSet.has(value));
4 }
5
6 intersection([1, 2, 3], [1, 3, 5]) // [1, 3]
1 const numOfAppearances = (array, value) => {
2 return array.reduce((acc, currVal) => currVal === value ? ++acc : acc, 0);
3 };
4
5 const count = numOfAppearances(['one', 'yes', 3, 'yes', false], 'yes'); // 2
1 const arr = ['', 0, 1, 'true', false, Infinity, undefined, null, NaN, 12n, -0];
2 const truthy = arr.filter(Boolean); // [ 1, 'true', Infinity, 12n ]
1 const arr = [1000, 1, 6, 2, 100, 1001, 201, 20, 305];
2 const sorted = arr.sort((a, b) => a - b);
3 console.log(sorted); // [ 1, 2, 6, 20, 100, 201, 305, 1000, 1001 ]
1 const len = 3;
2 const nElementsArray = Array.from(Array(len).keys());
3 console.log(nElementsArray) // [0, 1, 2]
1 array = [1, 2, 3]
2 reversed = array[::-1] # [3, 2, 1]
1 const arr = ["Hello", "World", "!"];
2 let index = Math.floor(Math.random() * arr.length);
3 arr[index] // random element
1 const uniqueArray = [...new Set([1, 2, 2, 3, 3, 3])] // [ 1, 2, 3 ]