프로그래머스/Javascript : 기초 문제 3일차

프로그래머스/Javascript : 기초 문제 3일차

문자열 섞기

문제

길이가 같은 두 문자열 str1str2가 주어집니다.

두 문자열의 각 문자가 앞에서부터 서로 번갈아가면서 한 번씩 등장하는 문자열을 만들어 return 하는 solution 함수를 완성해 주세요.

  • 제한사항
    • 1 ≤ str1의 길이 = str2의 길이 ≤ 10
      • str1str2는 알파벳 소문자로 이루어진 문자열입니다.

소스 코드

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = '';
let count = 0;

rl.on('line', (line) => {
  input = line.split(' ');
  count++;

  if (count > 1) {
    rl.close();
  }
}).on('close', () => {
  const answer = solution(input[0], input[1]);
  console.log(answer);
});

const solution = (str1, str2) => {
  let answer = '';
  for (let i = 0; i < str1.length; i++) {
    answer += str1[i] + str2[i];
  }

  return answer;
};

문자 리스트를 문자열로 변환하기

문제

문자들이 담겨있는 배열 arr가 주어집니다. arr의 원소들을 순서대로 이어 붙인 문자열을 return 하는 solution함수를 작성해 주세요.

  • 제한사항
    • 1 ≤ arr의 길이 ≤ 200
      • arr의 원소는 전부 알파벳 소문자로 이루어진 길이가 1인 문자열입니다.

소스 코드

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = '';

rl.on('line', (line) => {
  input = line.split(' ');
  rl.close();
}).on('close', () => {
  const answer = solution(input);
  console.log(answer);
});

const solution = arr => arr.join('');

문자열 곱하기

문제

문자열 my_string과 정수 k가 주어질 때, my_stringk번 반복한 문자열을 return 하는 solution 함수를 작성해 주세요.

  • 제한사항
    • 1 ≤ my_string의 길이 ≤ 100
    • my_string은 영소문자로만 이루어져 있습니다.
    • 1 ≤ k ≤ 100

소스 코드

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = '';

rl.on('line', (line) => {
  input = line.split(' ');
}).on('close', () => {
  const answer = solution(input[0], input[1]);
  console.log(answer);
});

const solution = (my_string, k) => {
  let answer = '';
  for (let i = 0; i < k; i++) {
    answer += my_string;
  }

  return answer;
};

더 크게 합치기

문제

연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.

12 ⊕ 3 = 123 3 ⊕ 12 = 312 양의 정수 a와 b가 주어졌을 때, ab와 2 * ab 중 더 큰 값을 return하는 solution 함수를 완성해 주세요.

단, abba가 같다면 ab를 return 합니다

  • 제한사항
    • 1 ≤ a, b < 10,000

소스 코드

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = '';

rl.on('line', (line) => {
  input = line.split(' ');
  rl.close();
}).on('close', () => {
  const answer = solution(input[0], input[1]);
  console.log(answer);
});

const solution = (a, b) => {
  const c = String(a);
  const d = String(b);

  return Number(c + d) > Number(d + c) ? Number(c + d) : Number(d + c);
};

두 수의 연산값 비교하기

문제

연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.

12 ⊕ 3 = 123 3 ⊕ 12 = 312 양의 정수 a와 b가 주어졌을 때, ab와 2 * a * b 중 더 큰 값을 return하는 solution 함수를 완성해 주세요.

단, ab와 2 * a * b가 같으면 ab를 return 합니다.

  • 제한사항
    • 1 ≤ a, b < 10,000

소스 코드

const solution = (a, b) => {
  const c = String(a);
  const d = String(b);

  return Number(c + d) > 2 * a * b ? Number(c + d) : 2 * a * b;
};

이 글이 도움이 되었나요?

신고하기
0분 전
작성된 댓글이 없습니다. 첫 댓글을 달아보세요!
    댓글을 작성하려면 로그인이 필요합니다.