[2021.07.14]

Darlene·2021년 7월 14일
0

문제 1)

놀이공원 놀이기구마다 키 제한이 있습니다. 키가 150cm 이상만 탈 수 있는 놀이기구가 있습니다.

입력으로 키가 주어지면 키가 150이 넘으면 YES를 틀리면 NO를 출력하는 프로그램을 작성하세요.

1. 요구사상 분석

150cm 이상만 기구를 탈 수 있다.

2. 요구사항을 구체적이고 절차적으로 변환하기

입력 키 값이 150 이상이면 YES
입력 키 값이 150 미만이면 NO

3. 코드로 작성하기

답안지 작성
function heightLimtProgram(height) {
  const cutline =150;

  if(height >= cutline) {
    return "YES";
  } return "NO";
}

console.log(heightLimtProgram(180));   // YES
console.log(heightLimtProgram(145));   // NO
console.log(heightLimtProgram(150));   // YES

문제 2)

국어, 수학, 영어 시험을 보았습니다. 친구들의 평균 점수를 구해주기로 했습니다.

공백으로 구분하여 세 과목의 점수가 주어지면 전체 평균 점수를 구하는 프로그램을 작성하세요. 단, 소숫점 자리는 모두 버립니다.

입력 : 20 30 40
출력 : 30

1. 요구사상 분석

평균 점수 구하기
공백으로 구분하여 입력값이 들어옴
소숫점 자리 모두 버리기

2. 요구사항을 구체적이고 절차적으로 변환하기

input 되는 값이 공백을 구분하여 들어오는 것을 분리하기
분리해서 새로운 배열에 담아 내고 각 각 배열 요소의 합에 배열길이로 나누기
새로운 배열로 담아 낸 요소들이 문자열이므로 숫자형으로 바꿔주기

3. 코드로 작성하기

답안지 작성
function averageProgram(input) {
  const score = input;
  const array = score.split(" ");
  let totalnumber = 0;

 for(let i = 0; i < array.length; i++) {
   totalnumber += Number(array[i]); 
 }
 return Math.floor(totalnumber/array.length);
}

console.log(averageProgram("20 30 40"));  // 30
console.log(averageProgram("50 60 40"));  // 50
console.log(averageProgram("80 180 25"));  // 95

😍 새롭게 알게 된 점

  • 소숫점 자리
    Math.floor() : 소수점 이하를 버림한다.
    Math.ceil() : 소수점 이하를 올림한다.
    Math.round() : 소수점 이하를 반올림한다.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/round

  • 문자형 -> 숫자형 변환
    parseInt() : 문자를 정수형 숫자로 변환해준다.
    parseFloat() : 문자를 실수형 숫자로 변환해준다.
    Nember() : 문자를 정수 & 실수형 숫자로 변환해준다.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Number
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/parseInt

🙏 회고

for문 조건 설정을 잘 하도록 하자!


문제 3)

공백으로 구분하여 두 숫자 a와 b가 주어지면, a의 b승을 구하는 프로그램을 작성하세요.

1. 요구사상 분석

공백 구분 두 숫자 입력값
a의 b승 구하기

2. 요구사항을 구체적이고 절차적으로 변환하기

input = "a b"
[a, b]로 분리해서 배열에 담아낸 다음 각 요소를 숫자형으로 변환하기

2의 4승 : 222*2 = 16

3. 코드로 작성하기

답안지 작성
function squareRootProgram(input) {
  const string = input;
  const twoNmbersArray = string.split(" ");

 return  squareRoot = parseInt(twoNmbersArray[0]) ** parseInt(twoNmbersArray[1]); 
}

console.log(squareRootProgram("2 4"));  // 16
console.log(squareRootProgram("3 5"));  // 243

😍 새롭게 알게 된 점

지수연산자(Exponentiation)
x ** y

console.log(3 ** 4);  // expected output: 81

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation


문제 4)

공백으로 구분하여 두 숫자가 주어집니다.
두 번째 숫자로 첫 번째 숫자를 나누었을 때 그 몫과 나머지를 공백으로 구분하여 출력하세요.

입력 : 10 2
출력 : 5 0

1. 요구사상 분석

주어진 두 수를 나누었을 때 몫과 나머지를 공백으로 구분하여 출력하기

2. 요구사항을 구체적이고 절차적으로 변환하기

공백 구분 두 숫자 입력값
a/b = 몫 ... 나머지
예를 들어 10 / 2 = 5 ... 0 이고, 8 / 5 = 1 ... 3 이다.

나누기 연산자, 나머지 연산자 사용하기

3. 코드로 작성하기

답안지 작성
function Program(input) {
  const string = input;
  const twoNmbersArray = string.split(" ");

  const share =  Math.floor(parseInt(twoNmbersArray[0])/ parseInt(twoNmbersArray[1])); 
  const rest = parseInt(twoNmbersArray[0])% parseInt(twoNmbersArray[1]); 

  return `${share} ${rest}`;
}

console.log(Program("10 2"));  // 5 0
console.log(Program("10 4"));  // 2 2
console.log(Program("15 5"));  // 3 0

문제 5)

단어가 입력되면 전부 대문자로 출력되는 프로그램을 만들어주세요.

입력 : mary
출력 : Mary

1. 요구사상 분석

대문자로 출력되어야 한다.

2. 요구사항을 구체적이고 절차적으로 변환하기

대소문자 변환하기 메소드 사용하기

3. 코드로 작성하기

답안지 작성
function Program(input) {
  return input.toUpperCase();
}

console.log(Program("mary"));  // MARY

😍 새롭게 알게 된 점

  • toUpperCase() 메서드는 문자열을 대문자로 변환해 반환한다.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

  • toLowerCase() 메서드는 문자열을 소문자로 변환해 반환한다.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase


0개의 댓글