๐ŸŸฃ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํšŒ๊ณ 

๊น€ํƒœ์šฑยท2023๋…„ 3์›” 25์ผ
0
post-thumbnail

๐Ÿ˜š ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #1


[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1: ์Œ์–‘ ๋”ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
์–ด๋–ค ์ •์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ •์ˆ˜๋“ค์˜ ์ ˆ๋Œ“๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด absolutes์™€ ์ด ์ •์ˆ˜๋“ค์˜ ๋ถ€ํ˜ธ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ๋ถˆ๋ฆฌ์–ธ ๋ฐฐ์—ด signs๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์‹ค์ œ ์ •์ˆ˜๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • absolutes์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    * absolutes์˜ ๋ชจ๋“  ์ˆ˜๋Š” ๊ฐ๊ฐ 1 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • signs์˜ ๊ธธ์ด๋Š” absolutes์˜ ๊ธธ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    * signs[i] ๊ฐ€ ์ฐธ์ด๋ฉด absolutes[i] ์˜ ์‹ค์ œ ์ •์ˆ˜๊ฐ€ ์–‘์ˆ˜์ž„์„, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์Œ์ˆ˜์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

absolutessignsresult
[4,7,12][true,false,true]9
[1,2,3][false,false,true]0

๋‚˜์˜ ํ’€์ด

function solution(absolutes, signs) {
  var answer = absolutes;
  //signs[i] ๊ฐ€ ์ฐธ์ด๋ฉด absolutes[i] ์˜ ์‹ค์ œ ์ •์ˆ˜๊ฐ€ ์–‘์ˆ˜์ž„์„, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์Œ์ˆ˜์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  for (let i = 0; i < signs.length; i++) {
    if (signs[i] != true) absolutes[i] *= -1;
  }
  return answer.reduce((a, c) => a + c);
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. answer ์—๋‹ค absolutes ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”
2. for ๋ฐ˜๋ณต์„ ํ†ตํ•ด sign[i] false๋ฉด -1์„ ๊ณฑํ•œ ๊ฐ’์„ ์žฌ ํ• ๋‹น
3. answer์˜ reduce ๋ฉ”์†Œ๋“œ๋กœ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜.

๋ฌธ์ œ์ 
๋งˆ์ง€๋ง‰ return์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ์Šต๊ด€์„ ๊ณ ์ณ๋ณด์ž.
๋ชจ๋“  ๋ฆฌํ„ด ๊ฐ’์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•ด์„œ ๋ฐ˜ํ™˜๋œ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—,,


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(absolutes, signs) {

    return absolutes.reduce((acc, val, i) => acc + (val * (signs[i] ? 1 : -1)), 0);
}

ํ’€์ด
1. reduce ๋ฉ”์„œ๋“œ ํ•จ์ˆ˜ ์ ์šฉ์— acc(๋ˆ„์‚ฐ๊ฐ’)+cur/val(ํ˜„์žฌ๊ฐ’)์„ ๊ณฑํ•ด์ฃผ๋Š” ์—ฐ์‚ฐ ์ ์šฉ
2. ์—ฐ์‚ฐ์— sign[i]? ์‚ผํ•ญ ์กฐ๊ฑด์„ ํ†ตํ•ด truthy๋ฉด 1์„ ๊ณฑํ•˜๊ณ  falsy๋ฉด -1์„ ๊ณฑํ•˜๊ฒŒ ํ•จ.
3. ์ดˆ๊ธฐ ๊ฐ’ 0์„ ์ ์šฉํ•˜์—ฌ acc๊ฐ’์˜ ๋ฐฐ์—ด๋กœ ๊ณ„์‚ฐ๋˜์ง€ ์•Š๊ฒŒ ์ ์šฉ.


#2

function solution(absolutes, signs) {
    let answer = 0;
    for (let i = 0; i < absolutes.length; i++) {
        signs[i] ? answer += absolutes[i] : answer -= absolutes[i]
    }
    return answer;
}

ํ’€์ด
1. answer๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
2. for ๋ฐ˜๋ณต๊ณผ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด signs[i]
์ธ๋ฑ์Šค ๊ฐ’์ด truthy๋ฉด answer์—๋‹ค absolutes[i]๋ฅผ ๋”ํ•œ ๊ฐ’์„ ์žฌํ• ๋‹น.
falsy๋ฉด absolutes[i] ๋บ€ ๊ฐ’์„ ์žฌํ• ๋‹น.


๐Ÿ˜š ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #2

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1: ์ œ์ผ ์ž‘์€ ์ˆ˜ ์ œ๊ฑฐํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
์ •์ˆ˜๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด, arr ์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ๋ฆฌํ„ดํ•˜๋ ค๋Š” ๋ฐฐ์—ด์ด ๋นˆ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ์—” ๋ฐฐ์—ด์— -1์„ ์ฑ„์›Œ ๋ฆฌํ„ดํ•˜์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด arr์ด [4,3,2,1]์ธ ๊ฒฝ์šฐ๋Š” [4,3,2]๋ฅผ ๋ฆฌํ„ด ํ•˜๊ณ , [10]๋ฉด [-1]์„ ๋ฆฌํ„ด ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • arr์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • ์ธ๋ฑ์Šค i, j์— ๋Œ€ํ•ด i โ‰  j์ด๋ฉด arr[i] โ‰  arr[j] ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

absolutesresult
[4,3,2,1][4,3,2]
[10][-1]

๋‚˜์˜ ํ’€์ด

function solution(arr) {
  var answer = arr.sort((a, b) => b - a).slice(0, -1);
  if (answer.length <= 1) answer.push(-1);
  return answer;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. sort ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•˜์—ฌ ์ตœ์†Œ ๊ฐ’์ด ์ œ์ผ ๋’ค๋กœ ์˜ฌ ์ˆ˜์žˆ๊ฒŒ ์ •๋ ฌ
2. slice ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ๊ฐ’์„ ์ถ”์ถœํ•œ ๋’ค ์ถ”์ถœ๋œ ๋ฐฐ์—ด ๋ฐ˜ํ™˜.
3. if ์กฐ๊ฑด์„ ํ†ตํ•ด answer์˜ ๊ธธ์ด๊ฐ€ 1์ดํ•˜๊ฐ€ ๋˜๋ฉด -1์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜.

๋ฌธ์ œ์ 
์ด ๊ณผ์ •์—์„œ sort๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชป ํ•จ.
๊ทธ ์ด์œ ๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ๋ฐ˜ํ™˜ ๋œ return์ด ์ •๋ ฌ๋˜์–ด ๋‚˜์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ตœ์†Œ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ฐฐ์—ด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด์—ˆ๋‹ค.

์ตœ์†Œ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์„ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ๋ฌด์—‡ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค Math.min ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•˜๋‹ค.


๊ฐœ์„ ๋œ ๋ฐฉ๋ฒ•.

function solution(arr) {
  var minValue = Math.min(...arr);
  var answer = arr.filter((e) => e !== minValue);
  if (answer.length === 0) answer.push(-1);
  return answer;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. Math.min ๋ฉ”์„œ๋“œ์™€ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์„ ํ†ตํ•ด min ์ธ์ž์— arr ๋ฐฐ์—ด์„ ์ „๋‹ฌ.
2. filter ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•œ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
3. if ์กฐ๊ฑด์—์„œ answer๊ธธ์ด๊ฐ€ 0์ด๋ฉด -1 ๋ฐฐ์—ด์— ํ• ๋‹นํ•˜์—ฌ ๋ฐ˜ํ™˜.


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(arr) {
    arr.splice(arr.indexOf(Math.min(...arr)),1);
    if(arr.length<1)return[-1];
    return arr;
}

ํ’€์ด
1.splice ๋ฉ”์„œ๋“œ ์•ˆ์— splice ๋ฉ”์„œ๋“œ์˜ start ๊ฐ’์œผ๋กœ ์ตœ๊ณ  ๊ฐ’์œผ๋กœ ์†ํ•ด์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„ start๋ฅผ ๊ธฐ์ค€ํ–ˆ๊ณ , ์‚ญ์ œํ•  ์š”์†Œ๋ฅผ 1๋กœ ํ•œ๋ฒˆ์— ์„ค์ •ํ•ด๋‚ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
2. ๋‹ค์Œ if ์กฐ๊ฑด์„ ํ†ตํ•ด ๋ฐฐ์—ด์— [-1]์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • ํ•˜์˜€๋‹ค.


๐Ÿ˜š ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #3

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1: ์—†๋Š” ์ˆซ์ž ๋”ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ ์ผ๋ถ€๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์—์„œ ์ฐพ์„ ์ˆ˜ ์—†๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ์ฐพ์•„ ๋”ํ•œ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค numbers์˜ ๊ธธ์ด โ‰ค 9
    - 0 โ‰ค numbers์˜ ๋ชจ๋“  ์›์†Œ โ‰ค 9
    - numbers์˜ ๋ชจ๋“  ์›์†Œ๋Š” ์„œ๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

numbersresult
[1,2,3,4,6,7,8,0]14
[5,8,4,0,6,7,9]6

๋‚˜์˜ ํ’€์ด

function solution(numbers) {
  var answer = [];
  var setArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  for (let i = 0; i <= setArr.length; i++) {
    if (setArr.indexOf([i]) != numbers.indexOf([i])) answer.push(setArr[i]);
  }
  return answer;

๊ตฌํ˜„ ์ ‘๊ทผ
1. ๋น„๊ตํ•œ ๋‚˜๋จธ์ง€ ๊ฐ’์„ ๋‹ด์„ ๋นˆ ๋ฐฐ์—ด answer์™€ ๋ชจ๋“  ๋ฐฐ์—ด์„ ๊ฐ€์ง„ setArr๋ฅผ ์„ ์–ธ
2. ํ•ด๋‹น ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜์—ฌ ๋น„๊ตํ•˜๋Š” ๋ฐฐ์—ด๊ณผ ์ˆœํšŒํ•˜๋Š” ๋ฐฐ์—ด ์†์—์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด
์ฒ˜์Œ์—” filter ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผํ•˜๋ ค ํ–ˆ๋Š”๋ฐ ์›ํ•˜๋˜ ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์•„ indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ.
๊ทธ๋Ÿฌ๋‚˜ ๋‘ ํ•จ์ˆ˜์—์„œ ๊ณ„์†ํ•ด์„œ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•œ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜๋˜์–ด ์š”์†Œ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„  ์–ด๋–ค ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”ํ•œ์ง€ gpt์—๊ฒŒ ๋ฌผ์–ด๋ดค๋‹ค,,

gpt๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ใ…ฃํ•ด์„œ๋Š” includes( )๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ๋‹ค.

MDN์—์„œ๋„ includes() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์ด ํŠน์ • ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.
๋ผ๊ณ  ๋‚˜์™€ ์žˆ์—ˆ๋‹ค,,

์ข€ ๋” ์ฐพ์•„๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ๋Šฅ๋ ฅ์„ ๊ธธ๋Ÿฌ์•ผ ๋˜๊ฒ ๋‹ค..


ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ๋งŒ ๋๋‚ด๊ธฐ์—” ๋„ˆ๋ฌด ์•„์‰ฌ์›Œ ๊ธฐ์กด์— ์ ์šฉํ•˜๊ณ  ์‹ถ์—ˆ๋˜ filter ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ includes ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ ์ ์šฉํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋  ๊ฒƒ ๊ฐ™์•„์„œ ์ ์šฉ ํ•ด๋ณด์•˜๋‹ค.


๊ทธ๋ ‡๊ฒŒ ๊ฐœ์„ ํ•˜๊ฒŒ ๋œ ์ฝ”๋“œ!

function solution(numbers) {
  var setArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  var answer = setArr.filter((e, i) => !numbers.includes(setArr[i]));
  // filter์˜ e๋Š” ์—˜๋ฆฌ๋จผํŠธ ์ด๋ฏ€๋กœ number.includes์—์„œ number ๋ฐฐ์—ด์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค ์œ„์น˜ ๋ฅผ ๋ฐ˜ํ™˜.
  return answer.reduce((a, c) => a + c);
}

filter() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ ์šฉ ๋ฐฉ์‹์€

 arr.filter(callback(element[, index[, array]])[, thisArg])
Copy to Clipboard

์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐฐ์—ด์˜ element๋ฅผ ์ˆœํšŒํ•˜์—ฌ ๋Œ๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ index๋ฅผ ๋Œ์•„์„œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๊ตฌ๋ฌธ์ด๋‹ค.

function solution(numbers) {
  var setArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  var answer = setArr.filter((e) => !numbers.includes(setArr[e]));
  // filter์˜ e๋Š” ์—˜๋ฆฌ๋จผํŠธ ์ด๋ฏ€๋กœ number.includes์—์„œ number ๋ฐฐ์—ด์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค ์œ„์น˜ ๋ฅผ ๋ฐ˜ํ™˜.  
  // [4, 8]
  return answer.reduce((a, c) => a + c);
}

์ฒ˜์Œ filter๋ฅผ ์ ์šฉํ•˜์—ฌ ์ˆœํšŒํ•  ๋•Œ ์ธ์ž์— e๊ฐ’๋งŒ ์ ์šฉ๋˜์–ด ๋ฐ˜ํ™˜๋œ ๊ฐ’์ด ๊ณ„์†ํ•ด์„œ [4,8]์ด๊ธธ๋ž˜ ์™œ์ง€ ์‹ถ์—ˆ๋Š”๋ฐ, ํ•ด๋‹น element๊ฐ€ ์†ํ•œ ์ธ๋ฑ์Šค์˜ ์ž๋ฆฌ ๊ฐ’์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ๊ฐœ์„ ๋œ ์ฝ”๋“œ์—์„œ๋Š”

  var answer = setArr.filter((e,i) => !numbers.includes(setArr[i]));
// [5, 9]

์ธ๋ฑ์Šค๋ฅผ ์ˆœํšŒํ•˜์—ฌ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์†ํ•ด์žˆ๋Š” element๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋‹ˆ
์›ํ•˜๋Š” [5,9] ๊ฒฐ๊ณผ ๊ฐ’์„ ์–ป๊ฒŒ ๋˜์—ˆ๋‹ค.


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(numbers) {
    return 45 - numbers.reduce((cur, acc) => cur + acc, 0);
}

ํ’€์ด
1. ๋น„๊ต ๋Œ€์ƒ์ธ ์ „์ฒด ๋ฐฐ์—ด์˜ ์ดํ•ฉ์—์„œ ๋น„๊ตํ•ด์•ผ ๋  ๋ฐฐ์—ด์˜ ์ดํ•ฉ์„ ๋นผ๊ฒŒ๋˜๋ฉด ์›ํ•˜๋Š” ๋‚˜๋จธ์ง€ ๊ฐ’์„ ์–ป๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค,, (์ฒœ์žฌ์ธ๊ฐ€..)

#2

function solution(numbers) {
    let answer = 0;

    for(let i = 0; i <= 9; i++) {
        if(!numbers.includes(i)) answer += i;
    }

    return answer;
}

ํ’€์ด
1. gpt์—์„œ ์•Œ๋ ค์ค€ ๊ฒƒ ์ฒ˜๋Ÿผ ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ๋žŒ๋“ค์€
ํŠน์ • ์š”์†Œ ๊ฐ’์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด includes( ) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
๋ฐฉ๋ฒ•์„ ์—ฐ๊ตฌํ•  ์ค„ ์•„๋Š” ๊ฐœ๋ฐœ์ž!

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