Algorithm
Hacker rank Problem Name: 10 Days of JavaScript -
Hacker rank Problem Link:https://www.hackerrank.com/challenges/js10-objects/problem?isFullScreen=true
Objective
In this challenge, we practice creating objects. Check out the attached tutorial for more details.
Task
Complete the function in the editor. It has two parameters: a and b. It must return an object modeling a rectangle that has the following properties:
- Length : This value is equal to a.
- width: This value is equal to b.
- perimeter: This value is equal to 2 (a + b).
- area: This value is equal to ab.
Note: The names of the object's properties must be spelled correctly to pass this challenge.
Input Format
The first line contains an integer denoting a.
The second line contains an integer denoting b.
Constraints
- 1 <= a,b <= 100
Sample Input 0
4
5
Sample Output 0
4
5
18
20
Explanation 0
Given a length of a = 4 and a width of b = 5 the Rectangle object's perameter is
4 + 4 + 5 + 5 = 18 and its area is 4*5 = 20
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
'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++];
}
/*
* Complete the Rectangle function
*/
function Rectangle(a, b) {
this.length=a;
this.width=b;
this.perimeter=2*(a+b);
this.area=a*b;
}
Copy The Code &
Try With Live Editor
Input
Output