File tree Expand file tree Collapse file tree 4 files changed +161
-0
lines changed
Hackerrank solutions/10 days of js Expand file tree Collapse file tree 4 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Calculate the area of a rectangle.
3
+ *
4
+ * length: The length of the rectangle.
5
+ * width: The width of the rectangle.
6
+ *
7
+ * Return a number denoting the rectangle's area.
8
+ **/
9
+ function getArea ( length , width ) {
10
+ let area ;
11
+ // Write your code here
12
+ area = length * width
13
+ return area ;
14
+
15
+ }
16
+
17
+ /**
18
+ * Calculate the perimeter of a rectangle.
19
+ *
20
+ * length: The length of the rectangle.
21
+ * width: The width of the rectangle.
22
+ *
23
+ * Return a number denoting the perimeter of a rectangle.
24
+ **/
25
+ function getPerimeter ( length , width ) {
26
+ let perimeter ;
27
+ // Write your code here
28
+ perimeter = 2 * ( length + width )
29
+ return perimeter ;
30
+ }
31
+
32
+
33
+ function main ( ) {
34
+ const length = + ( readLine ( ) ) ;
35
+ const width = + ( readLine ( ) ) ;
36
+
37
+ console . log ( getArea ( length , width ) ) ;
38
+ console . log ( getPerimeter ( length , width ) ) ;
39
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ process . stdin . resume ( ) ;
4
+ process . stdin . setEncoding ( 'utf-8' ) ;
5
+
6
+ let inputString = '' ;
7
+ let currentLine = 0 ;
8
+
9
+ process . stdin . on ( 'data' , inputStdin => {
10
+ inputString += inputStdin ;
11
+ } ) ;
12
+
13
+ process . stdin . on ( 'end' , _ => {
14
+ inputString = inputString . trim ( ) . split ( '\n' ) . map ( string => {
15
+ return string . trim ( ) ;
16
+ } ) ;
17
+
18
+ main ( ) ;
19
+ } ) ;
20
+
21
+ function readLine ( ) {
22
+ return inputString [ currentLine ++ ] ;
23
+ }
24
+
25
+ function getGrade ( score ) {
26
+ return 'FEDCBA' [ parseInt ( ( score > 0 ? score - 1 : 0 ) / 5 ) ] ;
27
+ }
28
+
29
+
30
+
31
+ function main ( ) {
32
+ const score = + ( readLine ( ) ) ;
33
+
34
+ console . log ( getGrade ( score ) ) ;
35
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ process . stdin . resume ( ) ;
4
+ process . stdin . setEncoding ( 'utf-8' ) ;
5
+
6
+ let inputString = '' ;
7
+ let currentLine = 0 ;
8
+
9
+ process . stdin . on ( 'data' , inputStdin => {
10
+ inputString += inputStdin ;
11
+ } ) ;
12
+
13
+ process . stdin . on ( 'end' , _ => {
14
+ inputString = inputString . trim ( ) . split ( '\n' ) . map ( string => {
15
+ return string . trim ( ) ;
16
+ } ) ;
17
+
18
+ main ( ) ;
19
+ } ) ;
20
+
21
+ function readLine ( ) {
22
+ return inputString [ currentLine ++ ] ;
23
+ }
24
+ /*
25
+ * Create the function factorial here
26
+ */
27
+ function factorial ( n ) {
28
+ let finalVal = 1 ;
29
+ for ( let i = n ; i > 1 ; i -- ) {
30
+ finalVal *= i ;
31
+ }
32
+ return finalVal ;
33
+ }
34
+
35
+
36
+ function main ( ) {
37
+ const n = + ( readLine ( ) ) ;
38
+
39
+ console . log ( factorial ( n ) ) ;
40
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ process . stdin . resume ( ) ;
4
+ process . stdin . setEncoding ( 'utf-8' ) ;
5
+
6
+ let inputString = '' ;
7
+ let currentLine = 0 ;
8
+
9
+ process . stdin . on ( 'data' , inputStdin => {
10
+ inputString += inputStdin ;
11
+ } ) ;
12
+
13
+ process . stdin . on ( 'end' , _ => {
14
+ inputString = inputString . trim ( ) . split ( '\n' ) . map ( string => {
15
+ return string . trim ( ) ;
16
+ } ) ;
17
+
18
+ main ( ) ;
19
+ } ) ;
20
+
21
+ function readLine ( ) {
22
+ return inputString [ currentLine ++ ] ;
23
+ }
24
+
25
+ function main ( ) {
26
+ // Write your code here. Read input using 'readLine()' and print output using 'console.log()'.
27
+
28
+ // Print the area of the circle:
29
+
30
+ // Print the perimeter of the circle:
31
+ let r = readLine ( ) ;
32
+ const PI = Math . PI ;
33
+
34
+ // Print the area of the circle:
35
+ console . log ( PI * r * r ) ;
36
+ // Print the perimeter of the circle:
37
+ console . log ( PI * 2 * r ) ;
38
+
39
+ try {
40
+ // Attempt to redefine the value of constant variable PI
41
+ PI = 0 ;
42
+ // Attempt to print the value of PI
43
+ console . log ( PI ) ;
44
+ } catch ( error ) {
45
+ console . error ( "You correctly declared 'PI' as a constant." ) ;
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments