Day 4: Create A Rectangle Object Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 4: Create A Rectangle Object Solution.
Also visit this link:ย Day 3: Throw Solution
Day 4: Create A Rectangle Object Solution
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++]; } /* ย *ย Completeย theย Rectangleย function ย */ 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); }