For Loop
for (let i = 0; i < 9; i++) {
console.log(i)
}
Hash
function solution(participant, completion) {
var answer = '';
const map = new Map();
completion.forEach(completePlayer => {
if (map.get(completePlayer))
map.set(completePlayer, map.get(completePlayer)+1)
else map.set(completePlayer, 1)
})
participant.forEach(participatedPlayer => {
if (!map.get(participatedPlayer))
answer = participatedPlayer
else map.set(participatedPlayer, map.get(participatedPlayer)-1)
})
return answer;
}
Set
let setA = new Set();
setA.add('a');
setA.add('b');
setA.add('a');
console.log([...setA.keys()]); // ['a', 'b']
console.log([...setA.values()]); // ['a', 'b']
List
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // 5
console.log(array1); // [4,5,1,2,3]
Sort
arr.sort(function(a, b) {
if(a > b) return 1;
if(a === b) return 0;
if(a < b) return -1;
});
arr.sort(function(a, b) {
return a - b
});
Math
Combinations
function combination(arr, selectNum) {
const result = [];
if (selectNum === 1) return arr.map((v) => [v]);
arr.forEach((v, idx, arr) => {
const fixed = v;
const restArr = arr.slice(idx + 1); //
const combinationArr = combination(restArr, selectNum - 1);
const combineFix = combinationArr.map((v) => [fixed, ...v]);
result.push(...combineFix);
});
return result;
}
Permutations
function permutation(arr, selectNum) {
let result = [];
if (selectNum === 1) return arr.map((v) => [v]);
arr.forEach((v, idx, arr) => {
const fixer = v;
const restArr = arr.filter((_, index) => index !== idx); //
const permuationArr = permutation(restArr, selectNum - 1);
const combineFixer = permuationArr.map((v) => [fixer, ...v]);
result.push(...combineFixer);
});
return result;
}