Day 4: Create a Rectangle Object in JavaScript | 10 Days Of JavaScript

Hello there, today we are going to solve Day 4: Create a Rectangle Object Hacker Rank Solution in JavaScript which is a Part of 10 Days Of JavaScript.

Hello there, today we are going to solve Day 4: Create a Rectangle Object Hacker Rank Solution in JavaScript which is a Part of 10 Days Of JavaScript Series.

Day 4: Create a Rectangle Object in JavaScript
Table Of Contents 👊

Objective

In this challenge, we practice creating objects.

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 a * b

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

Output Format

Return a object that has the properties specified above. Locked code in the editor prints the returned object's length, width, perimeter, and area to STDOUT.

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 perimeter is 4 + 4 +5 + 5 = 18 and its area is 4*5 = 20.

Solution - Day 4: Create a Rectangle Object

'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 Rectangle(a, b) {
    this.length = a;
    this.width = b;
    this.perimeter = 2 * (a + b);
    this.area = a * b;
}


function main() {
    const a = +(readLine());
    const b = +(readLine());
    
    const rec = new Rectangle(a, b);
    
    console.log(rec.length);
    console.log(rec.width);
    console.log(rec.perimeter);
    console.log(rec.area);
}

Disclaimer: The above Problem (Create a Rectangle Object) is generated by Hacker Rank but the Solution is Provided by Sloth Coders. This tutorial is only for Educational and Learning Purpose.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment