반응형

if else 문으로 범위에 맞는 알파벳을 출력시키는 문제이다.

처음에 아무 생각없이 숫자 다 넣었다가 고쳤더니 정답이었다.. ㅎ

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readLine() {
    return inputString[currentLine++];
}

function getGrade(score) {
    let grade;
    // Write your code here

    if(25 < score <= 30){
        grade = "A"
    }else if(20 < score <= 25){
        grade = "B"
    }else if(15 < score <= 20){
        grade = "C"
    }else if(10 < score <= 15){
        grade = "D"
    }else if(5 < score <= 10){
        grade = "E"
    }else{
        grade = "F"
    }
    
    return grade;
}

위처럼 범위를 다 지정해줬더니 "A"를 출력했다. (왜..? 아는분은 알려주세유..~)

그래서 간략하게 범위를 지정하였더니 정답!

 

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readLine() {
    return inputString[currentLine++];
}

function getGrade(score) {
    let grade;
    // Write your code here

    if(25 < score){
        grade = "A"
    }else if(20 < score){
        grade = "B"
    }else if(15 < score){
        grade = "C"
    }else if(10 < score){
        grade = "D"
    }else if(5 < score){
        grade = "E"
    }else{
        grade = "F"
    }
    
    return grade;
}
반응형
반응형

 

수식을 옮겨 적어도 되지 않아서

PI 값과 r 값을 담아주었다.

여기서 r 을 담는 readline()을 몰랐었는데 아래에서 잘 설명해주었다.

 

https://velog.io/@longlive-jonghan/JavaScript-%EC%9E%85%EC%B6%9C%EB%A0%A5readline-%EB%AA%A8%EB%93%88

 

JavaScript 콘솔에서 값 입출력(readline 모듈 기초)

readline 모듈은 JavaScript에 내장된 모듈로, readable 스트림에서 한 줄씩 입출력을 처리할 수 있게 도와줍니다.모듈을 불러오기 위해서는 require("모듈 이름")를 이용해야 합니다.모듈을 불러오고 readli

velog.io

 

⭐ readline 모듈은 JavaScript에 내장된 모듈로, readable 스트림에서 한 줄씩 입출력을 처리할 수 있게 도와줍니다.

 

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readLine() {
    return inputString[currentLine++];
}

function main() {
    // Write your code here. Read input using 'readLine()' and print output using 'console.log()'.
    
    // Print the area of the circle:
    const PI = Math.PI;
    let r = readLine();
    
    let area = r * r * PI
    
    // Print the perimeter of the circle:
 
    let perimeter = 2 * r * PI
    
    console.log(area)
    console.log(perimeter)
    try {

 

main 함수 안에 풀어주었다.

반응형
반응형
'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readLine() {
    return inputString[currentLine++];
}

/**
*   Calculate the area of a rectangle.
*
*   length: The length of the rectangle.
*   width: The width of the rectangle.
*   
*   Return a number denoting the rectangle's area.
**/
function getArea(length, width) {
    let area;
    // Write your code here
    area = length * width
    
    return area;
}

/**
*   Calculate the perimeter of a rectangle.
*   
*   length: The length of the rectangle.
*   width: The width of the rectangle.
*   
*   Return a number denoting the perimeter of a rectangle.
**/
function getPerimeter(length, width) {
    let perimeter;
    // Write your code here
    perimeter = 2 * (length + width)
    return perimeter;
}

문제에 나온대로

수식을 그대로 length, width로 옮겨 적었다.

반응형

'코딩테스트 > HackerRank' 카테고리의 다른 글

HackerRank day2 - Conditional Statements: If-Else  (0) 2022.12.14
HackerRank day1 - Let and Const  (0) 2022.12.14
HackerRank day1 - Let and Const  (0) 2022.02.04
HackerRank day1 - function  (0) 2022.01.17
HackerRank day0 - Data Types  (0) 2022.01.06
반응형

constant variable인 PI를 정의하고 r을 입력받아서 원의 넓이와 둘레를 출력

 

Math 함수를 몰라서 검색해 본 결과 =>

Math.PI 로 파이를 불러오고 Math.pow()로 제곱함수를 사용했다.

 

function main() {
    const PI = Math.PI;
    
    function getArea(r) {
        let area;
        area = PI * Math.pow(r, 2);
        //area 를 출력하기 위해 return
        return area;
    }
    function getPerimeter(r) {
        let perimeter;
        perimeter = 2 * PI * r;
        //위와 마찬가지로 return
        return perimeter;
    }
    
    const r = readLine();
    
    // Print the area of the circle:
    console.log(getArea(r))
    // Print the perimeter of the circle:
    console.log(getPerimeter(r))
 }
//es6 version
function main() {
    const PI = Math.PI;
    
    const getArea = (r) => PI * Math.pow(r, 2)
    const getPerimeter = (r) => 2 * PI * r;
    
    const r = readLine();
    
    // Print the area of the circle:
    console.log(getArea(r))
    // Print the perimeter of the circle:
    console.log(getPerimeter(r))
 }
반응형

'코딩테스트 > HackerRank' 카테고리의 다른 글

HackerRank day1 - Let and Const  (0) 2022.12.14
HackerRank day1 - Arithmetic Operators  (0) 2022.12.14
HackerRank day1 - function  (0) 2022.01.17
HackerRank day0 - Data Types  (0) 2022.01.06
HackerRank day0 - Hello world!  (0) 2022.01.06
반응형

4를 넣었을때 24가 나올 수 있게 하기

 

- 재귀함수를 이용해 안에서 함수를 또 선언해 조건이 끝날때까지 함수 호출

const factorial = (n) => (n - 1) > 0 ? n * factorial(n -1) : 1;

 

- 반복문을 이용한 해설

const factorial = (n) => {
    let result = n;
    for(let i = (n -1); i > 0; i--){
        result *= i
    }
    return(result);
}
반응형

'코딩테스트 > HackerRank' 카테고리의 다른 글

HackerRank day1 - Let and Const  (0) 2022.12.14
HackerRank day1 - Arithmetic Operators  (0) 2022.12.14
HackerRank day1 - Let and Const  (0) 2022.02.04
HackerRank day0 - Data Types  (0) 2022.01.06
HackerRank day0 - Hello world!  (0) 2022.01.06
반응형
/**
*   The variables 'firstInteger', 'firstDecimal', and 'firstString' are declared for you -- do not modify them.
*   Print three lines:
*   1. The sum of 'firstInteger' and the Number representation of 'secondInteger'.
*   2. The sum of 'firstDecimal' and the Number representation of 'secondDecimal'.
*   3. The concatenation of 'firstString' and 'secondString' ('firstString' must be first).
*
*	Parameter(s):
*   secondInteger - The string representation of an integer.
*   secondDecimal - The string representation of a floating-point number.
*   secondString - A string consisting of one or more space-separated words.
**/
function performOperation(secondInteger, secondDecimal, secondString) {
    // Declare a variable named 'firstInteger' and initialize with integer value 4.
    const firstInteger = 4;
    
    // Declare a variable named 'firstDecimal' and initialize with floating-point value 4.0.
    const firstDecimal = 4.0;
    
    // Declare a variable named 'firstString' and initialize with the string "HackerRank".
    const firstString = 'HackerRank ';
    
    // Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number        type) on a new line.
    console.log(parseInt(firstInteger) + parseInt(secondInteger));
    
    // Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number            type) on a new line.
    console.log(Number(firstDecimal) + Number(secondDecimal));
    
    // Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The        variable 'firstString' must be printed first.
    console.log(firstString + secondString);
}

 

정수와 소수, 문자열을 더해서(+) 출력시키는 문제인데

- 4 의 문자열을 parseInt 를 사용해서 정수로 변환하여 출력시키고

- 4.0 으로 되있는 소수점을 유지시키기 위해 Number 를 사용해서 숫자로 변환하여 출력시키고

- 문자열은 그대로 더해서 출력시켰다. 

반응형

'코딩테스트 > HackerRank' 카테고리의 다른 글

HackerRank day1 - Let and Const  (0) 2022.12.14
HackerRank day1 - Arithmetic Operators  (0) 2022.12.14
HackerRank day1 - Let and Const  (0) 2022.02.04
HackerRank day1 - function  (0) 2022.01.17
HackerRank day0 - Hello world!  (0) 2022.01.06
반응형
'use strict';

/**
*   A line of code that prints "Hello, World!" on a new line is provided in the editor. 
*   Write a second line of code that prints the contents of 'parameterVariable' on a new line.
*
*   Parameter:
*   parameterVariable - A string of text.
**/
function greeting(parameterVariable) {
    // This line prints 'Hello, World!' to the console:
    console.log('Hello, World!');

    // Write a line of code that prints parameterVariable to stdout using console.log:
    console.log(parameterVariable);
}

이 문제는 단순히 parameter 를 출력하면된다.

반응형

'코딩테스트 > HackerRank' 카테고리의 다른 글

HackerRank day1 - Let and Const  (0) 2022.12.14
HackerRank day1 - Arithmetic Operators  (0) 2022.12.14
HackerRank day1 - Let and Const  (0) 2022.02.04
HackerRank day1 - function  (0) 2022.01.17
HackerRank day0 - Data Types  (0) 2022.01.06

+ Recent posts