10 Days Of JavaScript

Day 0: Data Types Solution

Hello Friends! In this article, I’m going to share Hackerrank 10 Days of JavaScript solutions with you. Today, we’re going to solve the Day 0: Data Types challenge.

For more JavaScript solutions, you can also check out the Day 0: Hello World! Solution via the link.

Day 0: Data Types Solution

Objective:

In this challenge, you’ll learn about JavaScript data types. You’ll be performing some basic operations with different data types in JavaScript, including integers, decimals, and strings.

Task:

Variables named firstInteger, firstDecimal, and firstString are declared for you. You must use the + operator to perform the following operations:

  1. Convert secondInteger to an integer, sum it with firstInteger, and print the result.
  2. Convert secondDecimal to a floating-point number, sum it with firstDecimal, and print the result.
  3. Print the concatenation of firstString and secondString. Ensure firstString is printed first.

Input Format:

The function will receive:

  • string secondInteger: A string representation of an integer.
  • string secondDecimal: A string representation of a floating-point number.
  • string secondString: A string that you must append to firstString.

Output Format:

Print the following three lines of output:

  1. The sum of firstInteger and the integer representation of secondInteger.
  2. The sum of firstDecimal and the floating-point representation of secondDecimal.
  3. The concatenation of firstString and secondString, ensuring firstString appears first.

 

Sample Input 0:
12
4.32
is the best place to learn and practice coding!

Sample Output 0:
16
8.32
HackerRank is the best place to learn and practice coding!

 

Solution – Day 0: Data Types

Here’s the solution for the challenge:

'use strict';

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 ';

// Print the sum of 'firstInteger' and 'secondInteger' (converted to a Number type).
console.log(parseInt(secondInteger) + firstInteger);

// Print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number type).
console.log(parseFloat(secondDecimal) + firstDecimal);

// Print the concatenation of 'firstString' and 'secondString'.
console.log(firstString + secondString);
}

Explanation:

1) Summing Integers:

  • parseInt(secondInteger) converts the string secondInteger into an integer, and then adds it to the firstInteger.
    The result is printed using console.log().

2) Summing Decimals:

  • parseFloat(secondDecimal) converts the string secondDecimal into a floating-point number, and then adds it to firstDecimal.
    The sum of the two decimal numbers is printed.

3) String Concatenation:

  • The firstString is concatenated with secondString, ensuring that firstString is printed first. The result is printed on a new line.

 

Conclusion:

By following the above steps, you’ve successfully completed the Day 0: Data Types challenge. This challenge helps solidify your understanding of basic data type conversions and string manipulations in JavaScript.

Continue practicing to get more comfortable with JavaScript, and soon you’ll be solving more complex problems with ease!