๊ฐ์ค์น ์ ๋ ฌ์ ๋ฐ์ดํฐ์ ๋น์จ ์ด์ ์ซ์ ์์๊ฐ ์๋ ์ค์๋ ์์ผ๋ก ์ ๋ ฌํฉ๋๋ค.
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
Array.filter() ๋ฅผ ํตํด ๋ฐฐ์ด์ ์กด์ฌํ๋ ์ค๋ณต ์์๋ฅผ ์ ๊ฑฐํ ์ ์๋ค.
let filteredArray = nums.filter((item, index) => nums.indexOf(item) === index );
const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
console.log(obj.count);
// expected output: 42
console.log(obj.result);
// expected output: true
const rockPaperScissors = function () {
// TODO: ์ฌ๊ธฐ์ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
let arr = [];
for(let i = 0; i < 3; i++){
for(let j = 0; j < 3; j++){
for(let k = 0; k < 3; k++){
arr.push([i, j, k])
}
}
}
let sortedArr = arr.map(function(item){
return item.sort((a, b) => a - b);
});
let stringifiedArr = sortedArr.map(item => JSON.stringify(item));
let filteredArr = stringifiedArr.filter((item, index) => stringifiedArr.indexOf(item) === index);
let rearrayedArr = filteredArr.map(item => JSON.parse(item))
let result = [];
for (let item of rearrayedArr){
let miniArr = [];
for (let i = 0; i < item.length; i++){
if(item[i] === 0){
miniArr.push('rock')
} else if(item[i] === 1){
miniArr.push('paper')
} else if (item[i] === 2){
miniArr.push('scissors')
}
}
result.push(miniArr);
}
return result;
};
const rockPaperScissors = function () {
// TODO: ์ฌ๊ธฐ์ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
let arr = [];
for(let i = 0; i < 3; i++){
for(let j = 0; j < 3; j++){
for(let k = 0; k < 3; k++){
arr.push([i, j, k])
}
}
}
let result = [];
for (let item of arr){
let miniArr = [];
for (let i = 0; i < item.length; i++){
if(item[i] === 0){
miniArr.push('rock')
} else if(item[i] === 1){
miniArr.push('paper')
} else if (item[i] === 2){
miniArr.push('scissors')
}
}
result.push(miniArr);
}
return result;
};
let rockPaperScissors = function (n) {
n = n || 3
let result = [[]]
for(let i = 0; i < n; i++){
result = makeArr(result)
}
return result;
};
let makeArr = function(startArr){
let outsideArr = [];
for(let item of startArr){
let insideArr0 = [];
let insideArr1 = [];
let insideArr2 = [];
for(let el of item){
insideArr0.push(el);
}
insideArr0.push('rock')
outsideArr.push(insideArr0)
for(let el of item){
insideArr1.push(el);
}
insideArr1.push('paper')
outsideArr.push(insideArr1)
for(let el of item){
insideArr2.push(el);
}
insideArr2.push('scissors')
outsideArr.push(insideArr2)
}
return outsideArr;
}