데일리 코딩 07 (3개의 문자열 비교, 문자열 길이가 홀수인지 여부,템플릿 문자열, 절대값)

Numberbeen·2023년 1월 4일

Daily Coding

목록 보기
7/13
post-thumbnail

문제.1

차례대로 문자열 3개를 입력받아, 가장 짧은 문자열을 리턴해야 한다.

입력

인자 1 : word1

  • string 타입의 알파벳 문자열
  • word1.length 는 10 이하

인자 2 : word2

  • string 타입의 알파벳 문자열
  • word2.length 는 10 이하

인자 3 : word3

  • string 타입의 알파벳 문자열
  • word3.length 는 10 이하

출력

  • string 타입을 리턴해야 한다.

주의 사항

  • 동일한 길이의 문자열 중에서는 처음 입력받은 문자열을 리턴해야 한다.

입출력 예시

let output = findShortestOfThreeWords('a', 'two', 'three');
console.log(output); // --> 'a'

output = findShortestOfThreeWords('c', 'b', 'a');
console.log(output); // --> 'c'

정답

function findShortestOfThreeWords(word1, word2, word3) {
  // TODO: 여기에 코드를 작성합니다.
  shortsword = word1;
  if (word1.length > word2.length) {
    shortsword = word2;
    if (word2.length > word3.length) {
      shortsword = word3;
    }
  } else {
      if (word1.length > word3.length) {
        shortsword = word3;
        }
      }
    return shortsword;
  }

문제.2

문자열을 입력받아 그 길이가 홀수인지 여부를 리턴해야 한다.

입력

인자 1 : word

  • string 타입의 알파벳 문자열
  • word1.length 는 10 이하

출력

  • boolean 타입을 리턴해야 한다.

주의 사항

  • 0은 짝수로 간주한다.

입출력 예시

let output = isOddLength('special');
console.log(output); // --> true

output = isOddLength('specials');
console.log(output); // --> false

정답

function isOddLength(word) {
  if (word.length % 2 == 1 ) {
    return true;
  }
    return false;
}

문제.3

두 수를 입력받아 두 수의 차이를 나타내는 메세지를 리턴해야 한다.

입력

인자 1 : num1

  • number 타입의 정수 (num1 >= 0)

인자 2 : num2

  • number 타입의 정수 (num2 >= 0)

출력

  • string 타입을 리턴해야 한다.

주의 사항

  • 문자열 간 덧셈 연산은 금지된다.
  • Template String(Template literal)을 사용해 풀어야 한다.
  • 점수 간 차이는 절대값(absolute)을 사용한다.

입출력 예시

let output = computeDifference(3, 7);
console.log(output); // --> '3, 7의 차이는 4입니다.'

output = computeDifference(7, 3);
console.log(output); // --> '7, 3의 차이는 4입니다.'

정답

function computeDifference(num1, num2) {
  return `${num1}, ${num2}의 차이는 ${Math.abs(num1 - num2)}입니다.`;
}
profile
내기 이해한 것을 보관하는 곳

0개의 댓글