본문 바로가기
Web development/Algorithm

[HackerRank 30 Days of Code] Day 0 ~ Day 2

by 자몬다 2020. 7. 7.

Day 0

https://www.hackerrank.com/challenges/30-hello-world/problem

아주 간단한, 주어진 문자열을 로그로 찍는 문제다.

function processData(inputString) {
    // This line of code prints the first line of output
    console.log("Hello, World.");
    
    // Write the second line of output that prints the contents of 'inputString' here.
    console.log(inputString);
}

 

Day 1

https://www.hackerrank.com/challenges/30-data-types/problem

int, double, str가 하나 주어진다.

readLine으로 int, double, str을 추가로 읽어들인다.

주어진 int를 읽어들인 int형에 더해서 출력하고, 주어진 double은 읽어들인 double에 더해서 출력한다.

주어진 str엔 읽어들인 str에 더해서 출력한다.

주어진 double이 4.0처럼 소수점이 0으로 끝나기 때문에, toFixed()를 사용해야 했다.

    // Declare second integer, double, and String variables.
    const int = readLine()
    // Read and save an integer, double, and String to your variables.
    const double = readLine()
    const str = readLine()
    // console.log(int, double, str)
    // Print the sum of both integer variables on a new line.
    console.log(i + parseInt(int))
    // Print the sum of the double variables on a new line.
    console.log((parseFloat(d) + parseFloat(double)).toFixed(1))
    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    console.log(s + str)

 

 

Day2

https://www.hackerrank.com/challenges/30-operators/problem

숫자 3개가 주어진다. 차례로 음식값, 팁 비율, 세금 비율이다.

음식값 + 팁값 + 세금값을 더해 출력하면 된다.

소숫점은 반올림하여 출력하기 때문에 toFixed()를 사용했다.

function solve(meal_cost, tip_percent, tax_percent) {
    console.log((meal_cost/100*tip_percent + meal_cost/100*tax_percent + meal_cost).toFixed(0))
}

https://www.morolog.dev/entry/javascript-%EC%88%AB%EC%9E%90-%ED%98%95%EB%B3%80%ED%99%98%EB%AC%B8%EC%9E%90%EC%97%B4-Float?category=849635

float와 int 형변환에 대해서는 여기에 정리해두었다.

댓글