Learn Programming with CoffeeScript

코딩 따라가기 (2)

등수를 결정방법을 적는다.

알고있겠지만 등수는 저렇게 결정이 되다.

그렇다면, 일치하는 갯수를 구해서 몇개인지 판단하면 되겠다. 그런데 컴퓨터에게 '판단하라'고 지시하면 자동으로 결정해줄까?

그럴리 없다는것 알고 있을것이다.

갯수를 구해와서 6개인지 5개인지 4개인지 비교를 하고, 각 경우마다 올바른 등수를 결정하도록 지시해주어야 한다.

이런 문제를 지시하기 위해서 몇가지 명령어가 있다. 우선 비교 명령어로 is, isnt, >, < 등이 있는데,https://opentutorials.org/module/534/4723 여기서 설명을 보고 오기 바란다.

그리고 각 경우마다의 행동을 지시하는 if 명령어가있다.

if 명령어가 등장하면 컴퓨터는 소스에 적힌 내용을 한가지 규칙에 따라 결정한다.

우선 if 뒤에 적힌 명령을 수행하여 결과를 낸다.

그래서 이 결과가 인지 거짓인지에 따라서 어떤 명령어를 수행할지를 결정한다.

아래 같이 결과값에 따라 실행되는 내용이 달라진다.

if 실행하여_결과
  참일떄 실행 
  참일떄 실행 
else 
  거짓일떄 실행
  거짓일떄 실행

이러한 if는 몇가지 다른 형태로 쓸수 있는데, 거짓일떄 수행할 내용이 없을수 있다. 이때는 아래처럼 쓴다.

if 실행하여_결과
  참일떄 실행 
  참일떄 실행 

한편 참/거짓일떄 실행한 내용은 들여쓰기를 기준으로 결정함으로 공백을 잘 표기해야한다.

if 실행하여_결과
  참일떄 실행 
  참일떄 실행 
  참일떄 실행 
항상실행 - if 결과와 상관없이 항상실행
항상실행 - if 결과와 상관없이 항상실행
if 실행하여_결과
  참일떄 실행 
  참일떄 실행 
  참일떄 실행 
else 
  거짓일떄 실행
  거짓일떄 실행
항상실행 - if 결과와 상관없이 항상실행
항상실행 - if 결과와 상관없이 항상실행

그리고 else 뒤에 if를 이어 붙여서 앞선 결과가 거짓일떄 새로운 결과를 평가하여 실행여부를 정할수있다.

if 결과 A
  결과 A 참일떄 실행 
  결과 A 참일떄 실행 
  결과 A 참일떄 실행 
else if 결과 B
  결과 A 거짓이지만, 결과 B 참일떄 실행 
  결과 A 거짓이지만, 결과 B 참일떄 실행 
  결과 A 거짓이지만, 결과 B 참일떄 실행 
else
  결과 A 거짓, 결과 B 거짓일떄 실행
  결과 A 거짓, 결과 B 거짓일떄 실행
항상실행 - if 결과와 상관없이 항상실행
항상실행 - if 결과와 상관없이 항상실행

우리는 일치하는 갯수에 따라서 등수를 결정하면 됨으로 일치하는 갯수를 6,5,4,3으로 비교할 것이고, 5개일떄는 보너스도 비교할것이다.

WinNumber1 = 4
WinNumber2 = 12
WinNumber3 = 24
WinNumber4 = 33
WinNumber5 = 38
WinNumber6 = 45
WinNumberBonus = 22

GetMyRank = (N1,N2,N3,N4,N5,N6,NBonus)-> 

  cnt = GetCountOfMatchedNumber(N1,N2,N3,N4,N5,N6)

  if cnt is 6
    return 1
  else if cnt is 5
    if NBonus is WinNumberBonus
      return 2
    return 3
  else if cnt is 4
    return 4
  else if cnt is 3
    return 5
  else 
    return null

console.log GetMyRank 3,4,23,11,38,36,12

일치갯수를 GetCountOfMatchedNumber 명령(함수)를 실행시켜 알아내고자 하였다.

하지만 예상하겠지만, GetCountOfMatchedNumber를 정의하지 않았으니 에러를 낼것이다.

GetCountOfMatchedNumber 정의해보자.

WinNumber1 = 4
WinNumber2 = 12
WinNumber3 = 24
WinNumber4 = 33
WinNumber5 = 38
WinNumber6 = 45
WinNumberBonus = 22

GetMyRank = (N1,N2,N3,N4,N5,N6,NBonus)-> 

  cnt = GetCountOfMatchedNumber(N1,N2,N3,N4,N5,N6)

  if cnt is 6
    return 1
  else if cnt is 5
    if NBonus is WinNumberBonus
      return 2
    return 3
  else if cnt is 4
    return 4
  else if cnt is 3
    return 5
  else 
    return null

console.log GetMyRank 3,4,23,11,38,36,12

GetCountOfMatchedNumber = (N1,N2,N3,N4,N5,N6)->
  return 6

잘 실행될것으로 예상할수도 있겠지만,

이런 에러가 난다. undefined is not a function, 즉 undefined는 함수가 아닌데 함수처럼 써서 발생한 에러이다.

사실 커피스크립트는 위에서 부터 순차적으로 수행하기 때문에, GetMyRank 명령을 실행중에 GetCountOfMatchedNumber를 실행했는데, GetCountOfMatchedNumber는 한참 뒤에 정의를 알려주기 때문에 아직은 undefined 상태(= 미정의 상태)여서 발생하는 오류이다.

초보자가 보고 당황하기 쉬운 오류겠지만, 아래 원칙을 지키면 간단히 해결된다.

함수는 정의 먼저하고, 뒤에서 실행한다.

즉, 코드는 아래처럼순서를 바꾸어야한다.

WinNumber1 = 4
WinNumber2 = 12
WinNumber3 = 24
WinNumber4 = 33
WinNumber5 = 38
WinNumber6 = 45
WinNumberBonus = 22


GetCountOfMatchedNumber = (N1,N2,N3,N4,N5,N6)->
  return 6

GetMyRank = (N1,N2,N3,N4,N5,N6,NBonus)-> 

  cnt = GetCountOfMatchedNumber(N1,N2,N3,N4,N5,N6)

  if cnt is 6
    return 1
  else if cnt is 5
    if NBonus is WinNumberBonus
      return 2
    return 3
  else if cnt is 4
    return 4
  else if cnt is 3
    return 5
  else 
    return null

console.log GetMyRank 3,4,23,11,38,36,12

1등이라는 말도안되는 결과를 주겠지만 실행이 되었다.

이제 정확히 일치 숫자를 주게끔 고쳐야 겠다.

간단히 if문을 사용하여 갯수를 정확히 새면된다.

일치갯수를 구하도록 아래처럼 코딩하면 정확히 등수를 얻을수 있을 것이다.

GetCountOfMatchedNumber = (N1,N2,N3,N4,N5,N6)->

  cnt = 0
  if WinNumber1 is N1
    cnt = cnt + 1
  if WinNumber1 is N2
    cnt = cnt + 1
  if WinNumber1 is N3
    cnt = cnt + 1
  if WinNumber1 is N4
    cnt = cnt + 1
  if WinNumber1 is N5
    cnt = cnt + 1
  if WinNumber1 is N6
    cnt = cnt + 1

  if WinNumber2 is N1
    cnt = cnt + 1
  if WinNumber2 is N2
    cnt = cnt + 1
  if WinNumber2 is N3
    cnt = cnt + 1
  if WinNumber2 is N4
    cnt = cnt + 1
  if WinNumber2 is N5
    cnt = cnt + 1
  if WinNumber2 is N6
    cnt = cnt + 1


  if WinNumber3 is N1
    cnt = cnt + 1
  if WinNumber3 is N2
    cnt = cnt + 1
  if WinNumber3 is N3
    cnt = cnt + 1
  if WinNumber3 is N4
    cnt = cnt + 1
  if WinNumber3 is N5
    cnt = cnt + 1
  if WinNumber3 is N6
    cnt = cnt + 1



  if WinNumber4 is N1
    cnt = cnt + 1
  if WinNumber4 is N2
    cnt = cnt + 1
  if WinNumber4 is N3
    cnt = cnt + 1
  if WinNumber4 is N4
    cnt = cnt + 1
  if WinNumber4 is N5
    cnt = cnt + 1
  if WinNumber4 is N6
    cnt = cnt + 1



  if WinNumber5 is N1
    cnt = cnt + 1
  if WinNumber5 is N2
    cnt = cnt + 1
  if WinNumber5 is N3
    cnt = cnt + 1
  if WinNumber5 is N4
    cnt = cnt + 1
  if WinNumber5 is N5
    cnt = cnt + 1
  if WinNumber5 is N6
    cnt = cnt + 1


  if WinNumber6 is N1
    cnt = cnt + 1
  if WinNumber6 is N2
    cnt = cnt + 1
  if WinNumber6 is N3
    cnt = cnt + 1
  if WinNumber6 is N4
    cnt = cnt + 1
  if WinNumber6 is N5
    cnt = cnt + 1
  if WinNumber6 is N6
    cnt = cnt + 1

  return cnt

CONTINUE...

댓글

댓글 본문
버전 관리
Chunsik Hwang
현재 버전
선택 버전
graphittie 자세히 보기