Languages/JS

[프로그래밍 핵심 개념 in Javascript] 03. 제어문

Dev다D 2021. 2. 12. 18:49
반응형

서열 정리

 

한국에서는:

  1. 나이가 같은 경우: '친구'

  2. 자신보다 나이가 어릴 경우:

    2 - 1. 상대방 성별이 남성인 경우: '남동생',

    2 - 2. 상대방 성별이 여성인 경우: '여동생'

  3. 자신보다 나이가 많을 경우:

    3 - 1. 상대방 성별이 남성인 경우: '형',

    3 - 2. 상대방 성별이 여성인 경우: '누나'

라고 말하는 게 너무 어렵다고 합니다. 이런 Peter를 위해서 미리 자신의 나이와 성별을 입력해두고, 상대방의 나이와 성별을 전달하면 호칭을 판별해주는 whatShouldICallYou함수를 만들어줍시다.

코드를 잘 작성했다면, 다음과 같은 결과가 출력되어야 합니다.

 

여동생
남동생
친구

누나

// 나의 나이와, 나의 성별을 저장하는 변수
let myAge = 26;
let myGender = 'male';

// 호칭을 담은 변수
let callOlderBrother = '형';
let callOlderSister = '누나';
let callFriend = '친구';
let callYoungerSister = '여동생';
let callYoungerBrother = '남동생';

// 상대방의 나이와 성별에 따른 호칭을 리턴하는 함수 whatShouldICall를 완성하세요.
function whatShouldICallYou(yourAge, yourGender) {
    if (myAge === yourAge) {
        return callFriend;
    } else if (myAge > yourAge && myGender === yourGender) {
        return callYoungerBrother;
    } else if (myAge > yourAge && myGender !== yourGender) {
        return callYoungerSister;
    } else if (myAge < yourAge && myGender === yourGender) {
        return callOlderBrother;
    } else {
        return callOlderSister
    }
}

// 테스트 코드
let result1 = whatShouldICallYou(25, 'female');
let result2 = whatShouldICallYou(20, 'male');
let result3 = whatShouldICallYou(26, 'female');
let result4 = whatShouldICallYou(30, 'male');
let result5 = whatShouldICallYou(31, 'female');

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);
console.log(result5);

본 내용은 Codeit의  '프로그래밍 기초 in JavaScript' 강의를
참고하여 작성한 내용입니다.
반응형