Home ๊ฐ๋„๊ธฐ
Post
์ทจ์†Œ

๊ฐ๋„๊ธฐ

๐Ÿ”– ๊ฐ๋„๊ธฐ

๐Ÿ“Œ ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

๊ฐ์—์„œ 0๋„ ์ดˆ๊ณผ 90๋„ ๋ฏธ๋งŒ์€ ์˜ˆ๊ฐ, 90๋„๋Š” ์ง๊ฐ, 90๋„ ์ดˆ๊ณผ 180๋„ ๋ฏธ๋งŒ์€ ๋‘”๊ฐ 180๋„๋Š” ํ‰๊ฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ angle์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ์˜ˆ๊ฐ์ผ ๋•Œ 1, ์ง๊ฐ์ผ ๋•Œ 2, ๋‘”๊ฐ์ผ ๋•Œ 3, ํ‰๊ฐ์ผ ๋•Œ 4๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

  • ์˜ˆ๊ฐ : 0 < angle < 90
  • ์ง๊ฐ : angle = 90
  • ๋‘”๊ฐ : 90 < angle < 180
  • ํ‰๊ฐ : angle = 180

์ œํ•œ์‚ฌํ•ญ
  • 0 < angle โ‰ค 180
  • angle์€ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ
angleresult
701
913
1804

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • angle์ด 70์ด๋ฏ€๋กœ ์˜ˆ๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • angle์ด 91์ด๋ฏ€๋กœ ๋‘”๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 3์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • angle์ด 180์ด๋ฏ€๋กœ ํ‰๊ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 4๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
function solution(angle) {
    var answer = 0;
    if(angle>0 && angle<90)
        answer = 1;
    else if(angle === 90)
        answer = 2;
    else if(angle>90 && angle<180)
        answer = 3;
    else if(angle === 180)
        answer = 4;
    return answer;
}

์˜ˆ๊ฐ, ์ง๊ฐ, ๋‘”๊ฐ, ํ‰๊ฐ์€ ์กฐ๊ฑด์ด ์žˆ์œผ๋ฏ€๋กœ if ์กฐ๊ฑด๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ํ’€๊ฒŒ๋˜์—ˆ๋‹ค. ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ํ•œ์ค„์€ {}์™€ return์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ” ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

1
2
3
4
// ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด
function solution(angle) {
    return [0, 90, 91, 180].filter(x => angle>=x).length;
}

๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด์—์„œ๋Š” ๊ฐ๋„๋ณ„๋กœ ์ €์žฅํ•ด๋†“์€ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด filter()๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. ๋ฐ˜ํ™˜๊ฐ’ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜์˜€๋‹ค. ๊ฐ™์€ ๋ฌธ์ œ์—์„œ ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ๊ตฌ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฒŒ ์ฐฝ์˜๋ ฅ์ด ๋†’๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค.

  • filter()
    • ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    • arr.filter(callback(element[, index[, array]])[, thisArg])

๐Ÿ’ป ์ถœ๋ ฅ ๊ฒฐ๊ณผ

image-20221123121602426

This post is licensed under CC BY 4.0 by the author.

์ง์ˆ˜ ํ™€์ˆ˜ ๊ฐœ์ˆ˜

์–‘๊ผฌ์น˜