Toy Problem01 โœŠ๐Ÿผโœ‹๐ŸผโœŒ๐Ÿผ

9rganizedChaosยท2021๋…„ 1์›” 25์ผ
0

๊ฐ€์ค‘์น˜ ์ •๋ ฌ

๊ฐ€์ค‘์น˜ ์ •๋ ฌ์€ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ ์—ด์„ ์ˆซ์ž ์ˆœ์„œ๊ฐ€ ์•„๋‹Œ ์ค‘์š”๋„ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

.sort()

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 );
  • indexOf() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์—์„œ ์ง€์ •๋œ ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

๋ฐฐ์—ด ๋น„๊ต

JSON.parse()

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;
}
  • ์–ด๋“œ๋ฐด์Šค๋“œ ์š”๊ฑด๊นŒ์ง€ ํ†ต๊ณผ
  • ์žฌ๊ท€ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๊ธฐ
profile
๋ถ€์ •ํ™•ํ•œ ์ •๋ณด๋‚˜ ์ž˜๋ชป๋œ ์ •๋ณด๋Š” ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๋น ๋ฅด๊ฒŒ ์ˆ˜์ •ํ† ๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

0๊ฐœ์˜ ๋Œ“๊ธ€