10 Days of JavaScript Hackerrank Solutions — I

Overview: 10 Days of JavaScript

This series focuses on learning and practicing JavaScript. Each challenge comes with a tutorial article, and you can view these articles by clicking either the Topics tab along the top or the article icon in the right-hand menu.

Day 0: Hello, World!

A greeting function is provided for you in the editor below. It has one parameter, . Perform the following tasks to complete this challenge:

  1. Use console.log() to print Hello, World! on a new line in the console, which is also known as stdout or standard output. The code for this portion of the task is already provided in the editor.
  2. Use console.log() to print the contents of (i.e., the argument passed to main).

You’ve got this!

Input Format

Data TypeParameterDescriptionstringA single line of text containing one or more space-separated words.

Output Format

Print the following two lines of output:

  1. On the first line, print Hello, World! (this is provided for you in the editor).
  2. On the second line, print the contents of .

Sample Input 0

Welcome to 10 Days of JavaScript!

Sample Output 0

Hello, World!
Welcome to 10 Days of JavaScript!

Explanation 0

We printed two lines of output:

  1. We printed the literal string Hello, World! using the code provided in the editor.
  2. The value of passed to our main function in this Sample Case was Welcome to 10 Days of JavaScript!. We then passed our variable to console.log, which printed the contents of .
'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++];
}
/**
* 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!');
console.log(parameterVariable)
// Write a line of code that prints parameterVariable to stdout using console.log:

}
function main() {
const parameterVariable = readLine();

greeting(parameterVariable);
}

Day 0: Data Types

Variables named firstInteger, firstDecimal, and firstString are declared for you in the editor below. You must use the operator to perform the following sequence of operations:

  1. Convert secondInteger to an integer (Number type), then sum it with firstInteger and print the result on a new line using console.log.
  2. Convert secondDecimal to a floating-point number (Number type), then sum it with firstDecimal and print the result on a new line using console.log.
  3. Print the concatenation of firstString and secondString on a new line using console.log. Note that firstString must be printed first.

Input Format

Output Format

Print the following three lines of output:

  1. On the first line, print the sum of firstInteger and the integer representation of secondInteger.
  2. On the second line, print the sum of firstDecimal and the floating-point representation of secondDecimal.
  3. On the third line, print firstString concatenated with secondString. You must printfirstString before secondString.

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!

Explanation 0

When we sum the integers 4 and 12, we get the integer 16. 
When we sum the floating-point numbers 4.0 and 4.32, we get 8.32. When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!.

You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above.

'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++];
}
/**
* 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(secondInteger) + parseInt(firstInteger));
// 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(parseFloat(secondDecimal) + parseFloat(firstDecimal));
// 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)
}
function main() {
const secondInteger = readLine();
const secondDecimal = readLine();
const secondString = readLine();

performOperation(secondInteger, secondDecimal, secondString);
}

Day 1: Arithmetic Operators

Complete the following functions in the editor below:

  1. getArea(length, width): Calculate and return the area of a rectangle having sides length and width.
  2. getPerimeter(length, width): Calculate and return the perimeter of a rectangle having sides length and width.

The values returned by these functions are printed to stdout by locked stub code in the editor.

Input Format

Constraints

  • 1 ≤ length ,width ≤ 1000
  • length and width are scaled to at most three decimal places.

Output Format

Sample Input 0

3
4.5

Sample Output 0

13.5
15

Explanation 0

The area of the rectangle is length x width = 3 x 4.5 = 13.5.
The perimeter of the rectangle is 2.(length + width) = 2. (3 + 4.5) = 15.

'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

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

return perimeter;
}
function main() {
const length = +(readLine());
const width = +(readLine());

console.log(getArea(length, width));
console.log(getPerimeter(length, width));
}

Day 1: Functions

Implement a function named factorial that has one parameter: an integer, n. It must return the value of n!(i.e., n factorial).

Input Format

Locked stub code in the editor reads a single integer, , from stdin and passes it to a function named factorial.

Constraints

  • 1 ≤ n ≤ 10

Output Format

The function must return the value of n!.

Sample Input 0

4

Sample Output 0

24

Explanation 0

We return the value of 4! = 4 x 3 x 2 x 1 = 24.

'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++];
}
/*
* Create the function factorial here
*/
function factorial(x)
{ if (x === 0){
return 1;
}
return x * factorial(x-1);
}
function main() {
const n = +(readLine());

console.log(factorial(n));
}

Day 1: Let and Const

Task

  1. Declare a constant variable, PI , and assign it the value Math.PI. You will not pass this challenge unless the variable is declared as a constant and named PI (uppercase).
  2. Read a number, r , denoting the radius of a circle from stdin.
  3. Use PI and r to calculate the area and perimeter of a circle having radius r.
  4. Print area as the first line of output and print perimeter as the second line of output.

Input Format

A single integer, r , denoting the radius of a circle.

Constraints

  • 0 < r ≤ 100
  • r is a floating-point number scaled to at most 3 decimal places.

Output Format

Print the following two lines:

  1. On the first line, print the area of the circle having radius r.
  2. On the second line, print the perimeter of the circle having radius r.

Sample Input 0

2.6

Sample Output 0

21.237166338267002
16.336281798666924

Explanation 0

Given the radius r= 2.6 , we calculate the following:

  • area = π.(r x r) = 21.2377166338267002
  • perimeter = 2.π.r = 16.336281798666924

We then print area as our first line of output and perimeter as our second line of output.

'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()'.
var a = readLine();

// Print the area of the circle:
console.log(Math.PI * a * a);
// Print the perimeter of the circle:
console.log(2 * Math.PI * a);

try {
// Attempt to redefine the value of constant variable PI
PI = 0;
// Attempt to print the value of PI
console.log(PI);
} catch(error) {
console.error("You correctly declared 'PI' as a constant.");
}
}