DEV Community

pranav589
pranav589

Posted on

Day-1: 30 days of code- Hackerrank

Alt Text

Hello guys!! Continuing the 30 days of code challenge by hackerrank using javascript, today we are solving the question related to 'data types'.

What are different types??

Data can be of different types like Strings, Numbers, Arrays, Objects, etc.

Lets dive into the solution.

Day-1

Task:-

'Complete the code in the editor below. The variables i,d ,s and are already declared and initialized for you. You must:

  • Declare 3 variables: one of type int, one of type double, and one of type String.
  • Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your 3 variables.
  • Use the + operator to perform the following operations:
    • Print the sum of i plus your int variable on a new line.
    • Print the sum of d plus your double variable to a scale of one decimal place on a new line.
    • Concatenate s with the string you read as input and print the result on a new line.'

Solution:-

function main() {
    var i = 4
    var d = 4.0
    var s = "HackerRank "
    // Declare second integer, double, and String variables.
    var a,b,c
    // Read and save an integer, double, and String to your variables.
    a=+(readLine())
    b=+(readLine())
    c=readLine()
    // Print the sum of both integer variables on a new line.
    console.log(i+a)
    // Print the sum of the double variables on a new line.
    console.log((d+b).toFixed(1))
    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    console.log(s+c)
}
Enter fullscreen mode Exit fullscreen mode

Explanation:-

Initially some variables are already declared and initialized for us.(i, d, s)

  1. Here we have declared our variables(a, b, c) a=integer b=double c=string
  2. Then we read the input given by the user using readLine() function and store it into the variables that we have declared above i.e., a, b, c.
  3. As specified in the task, we have to do sum of the two integers i.e., i and a.
  4. Then we add d and b. Here we have used a toFixed() method. By using the method, we can specify the number of decimals we want.
  5. And the last task is to concatenate the strings using the + operator.

Topics Covered:-

  1. Data types
  2. Concatenation
  3. Declaring variables and assigning values to it.
  4. toFixed() method

Thank you guys !!! Stay tuned!!

Top comments (0)