문제 1

문자열  str 이 주어질 때,  str 을 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
const readline = require('readline');
cosnt rl = readline.createInterface({
	input: process.stdin,
    output: process.stdout
}

let input = [];

rl.on( 'line', function(line){
	input = [line];
}).on( 'close', function(){
	str = input[0];
    console.log(str)
});

 

 다른 유저가 푼 방식

// 유저 1
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', console.log)

 

문제 2

주어진 문자열 과 정수를 이용하여 정수만큼 문자열을 반복하여 출력하세요

 

▶ 내가 푼 방식

//내가 작성한 코드
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    str = input[0];
    n = Number(input[1]);
    result = [];
    for(let i =0; i<n; i++){
        result.push(str)
    }
    console.log(result.join(''))
});

 

 다른 유저가 푼 방식

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    str = input[0];
    n = Number(input[1]);
    console.log(str.repeat(n));
});

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    str = input[0];
    n = Number(input[1]);
    for (i=0; i<n; i++){
        process.stdout.write(str);   
    }

});


// 유저 3

 

 배운 것들

     - repeat 함수로 반복 가능

     - process.stdout.write() 도 가능 

 

문제 3 대문자소문자 바꿔서 출력하기

영어 알파벳으로 이루어진 문자열  str 이 주어집니다.
각 알파벳을 대문자는 소문자로 소문자는 대문자로 변환해서 출력하는 코드를 작성해 보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = [line];
}).on('close',function(){
    str = input[0];
    for(let i =0; i < str.length; i ++){
        const word = /[A-Z]/.test(str[i]) ? str[i].toLowerCase() : str[i].toUpperCase();
        process.stdout.write(word)
    }
});

 

 다른 유저가 푼 방식

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

let input;

rl.on('line', (line) => {
  input = [...line];
}).on('close', () => {
  console.log(
    input.map((char) => (/[a-z]/.test(char) ? char.toUpperCase() : char.toLowerCase())).join(''),
  );
});

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

let input = [];

rl.on('line', function (line) {
    input = [line];
}).on('close',function(){
    str = input[0];
    const regex = /[A-Z]/
    console.log([...str].map((v)=> regex.test(v) ? v.toLowerCase() : v.toUpperCase()).join(''))
});

 

 배운 것

     -  스프레드 방식으로 문자를 배열로 만들수 있다는 점

 

문제 4

!@#$%^&*(\'"<>?:; 출력하기

 

▶ 내가 푼 방식

//내가 작성한 코드
// !@#$%^&*(\'"<>?:; 을 출력하기 위해서 \ 추가
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input;
rl.on('close', function () {
    const output = `!@#$%^&*(\\'"<>?:;`
    process.stdout.write(output)
    
});

 

문제 1

머쓱이는 할머니께 생신 축하 편지를 쓰려고 합니다. 할머니가 보시기 편하도록 글자 한 자 한 자를 가로 2cm 크기로 적으려고 하며, 편지를 가로로만 적을 때, 축하 문구 message 를 적기 위해 필요한 편지지의 최소 가로길이를 return 하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(message) {
    return message.length*2;
}

 

 다른 유저가 푼 방식

// 유저 1
function solution(message) {
    var horizontal = null;
    if((typeof message)=="string" ){
        horizontal = message.length * 2;
    }

    return horizontal;
}

 

 배운 것들

     -  무

문제 1

정수가 담긴 배열 array와 정수 n이 매개변수로 주어질 때, array에 n이 몇 개 있는 지를 return 하도록 solution 함수를 완성해보세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(array, n) {
    return array.filter((a) => a === n ).length;
}

 

 다른 유저가 푼 방식

// 유저 1
function solution(array, n) {
    var answer = 0;
    for(var i = 0; i < array.length; i++){
        if(array[i] == n){
            answer++
        }
    }
    return answer;
}

// 유저 2
function solution(array, n) {
    return array.reduce((prev, curr) => {
        if(curr === n) prev++;
        return prev;
    }, 0);
}

// 유저 3
function solution(array, n) {
    var answer = 0;
    let mapArr = array.map((x) => {
        if(x === n){
        return  answer++
        }
    });
    return answer;
}

 

 배운 것들

     -  무

문제

문자열 배열 strlist가 매개변수로 주어집니다. strlist 각 원소의 길이를 담은 배열을 retrun하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드

function solution(strlist) {
    var answer = [];
    for( let i of strlist){
        answer.push(i.length)
    }
    return answer;
}

// 간단한 버전
const solution = strlist => strlist.map(i => i.length);

 

 다른 유저가 푼 방식

// 유저 1
function solution(strlist) {
	return strlist.map(i => i.length)
}

// 유저 2 : reduce 이용
function solution(strlist) {
    return strlist.reduce((a, b) => [...a, b.length], [])
}

// 유저 3 : foreach 이용
function solution(strlist) {
    var answer = [];
    strlist.forEach(el=>answer.push(el.length))
    return answer;
}

 

 배운 것들

     - reduce

array.reduce(function(accumulator, currentValue, currentIndex, array), initialValue)

 

문제 내용

정수 number와 n, m이 주어집니다.
number가 n의 배수이면서 m의 배수이면 1을 아니라면 0을 return하도록 solution 함수를 완성해주세요.

 

▶ 내가 푼 방식

//내가 작성한 코드
function solution(number, n, m) {
    if (number % n ===0 && number % m === 0){
        return 1;
    }
    if (number % n !==0 || number % m !== 0){
        return 0;
    }
}

 

 다른 유저가 푼 방식

// 유저 1
function solution(number, n, m) {
  return +!(number % n || number % m);
}

// 유저 2
function solution(number, n, m) {
    return (number%n ===0) ? (number%m===0) ? 1 : 0 : 0;
}

// 유저 3
function solution(number, n, m) {
    return number % n === 0 && number % m === 0 ? 1 : 0
}

 

 배운 것들

     -  삼항 연산자 복습

const result = (조건식) ? (참일 때의 값) : (거짓일 때의 값)

+ Recent posts