File tree Expand file tree Collapse file tree 4 files changed +168
-0
lines changed
Hackerrank solutions/10 days of js Expand file tree Collapse file tree 4 files changed +168
-0
lines changed 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
+ /**
26
+ * Return the second largest number in the array.
27
+ * @param {Number[] } nums - An array of numbers.
28
+ * @return {Number } The second largest number in the array.
29
+ **/
30
+ function getSecondLargest ( nums ) {
31
+ // Complete the function
32
+ let first = nums [ 0 ] ;
33
+ let second = - 1 ;
34
+ for ( let i = 0 ; i < nums . length ; i ++ )
35
+ {
36
+ if ( nums [ i ] > first )
37
+ {
38
+ second = first ;
39
+ first = nums [ i ] ;
40
+ }
41
+ if ( nums [ i ] > second && nums [ i ] < first )
42
+ {
43
+ second = nums [ i ] ;
44
+ }
45
+ }
46
+ return second ;
47
+ }
48
+
49
+
50
+ function main ( ) {
51
+ const n = + ( readLine ( ) ) ;
52
+ const nums = readLine ( ) . split ( ' ' ) . map ( Number ) ;
53
+
54
+ console . log ( getSecondLargest ( nums ) ) ;
55
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Implement a Polygon class with the following properties:
3
+ * 1. A constructor that takes an array of integer side lengths.
4
+ * 2. A 'perimeter' method that returns the sum of the Polygon's side lengths.
5
+ */
6
+ class Polygon {
7
+ constructor ( sides ) {
8
+ this . sides = sides
9
+ }
10
+ perimeter ( ) {
11
+ return this . sides . reduce ( function add ( a , b ) { return a + b ; } )
12
+ }
13
+ }
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 getLetter ( s ) {
26
+
27
+ switch ( s . charAt ( 0 ) )
28
+ {
29
+ case ( 'a' || 'e' || 'i' || 'o' || 'u' ) :
30
+ return 'A'
31
+ case ( 'b' || 'c' || 'd' || 'f' || 'g' ) :
32
+ return 'B'
33
+ case ( 'h' || 'j' || 'k' || 'l' || 'm' ) :
34
+ return 'C'
35
+ case ( 'z' || 'n' || 'p' || 'q' || 'r' || 's' || 't' || 'v' || 'w' || 'x' || 'y' ) :
36
+ return 'D'
37
+
38
+ }
39
+ }
40
+
41
+
42
+ function main ( ) {
43
+ const s = readLine ( ) ;
44
+
45
+ console . log ( getLetter ( s ) ) ;
46
+ }
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
+ /*
26
+ * Complete the vowelsAndConsonants function.
27
+ * Print your output using 'console.log()'.
28
+ */
29
+ function vowelsAndConsonants ( s ) {
30
+ //for vowels
31
+ for ( let ind = 0 ; ind < s . length ; ind ++ ) {
32
+ if ( s . charAt ( ind ) . match ( / [ a e i o u ] / ) ) {
33
+ console . log ( s . charAt ( ind ) ) ;
34
+ }
35
+ }
36
+ //for consonants
37
+
38
+ for ( let ind = 0 ; ind < s . length ; ind ++ ) {
39
+ if ( s . charAt ( ind ) . match ( / [ ^ a e i o u ] / ) ) {
40
+ console . log ( s . charAt ( ind ) ) ;
41
+ }
42
+ }
43
+ }
44
+
45
+
46
+
47
+
48
+
49
+
50
+ function main ( ) {
51
+ const s = readLine ( ) ;
52
+
53
+ vowelsAndConsonants ( s ) ;
54
+ }
You can’t perform that action at this time.
0 commit comments