File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Hackerrank solutions/10 days of js Expand file tree Collapse file tree 1 file changed +49
-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
+ * Modify and return the array so that all even elements are doubled and all odd elements are tripled.
27
+ *
28
+ * Parameter(s):
29
+ * nums: An array of numbers.
30
+ */
31
+ function modifyArray ( nums ) {
32
+ var something = function ( n ) {
33
+ if ( n % 2 == 0 )
34
+ return n * 2 ;
35
+ else
36
+ return n * 3 ;
37
+
38
+ }
39
+ var newArray = nums . map ( something ) ;
40
+ return newArray ;
41
+ }
42
+
43
+
44
+ function main ( ) {
45
+ const n = + ( readLine ( ) ) ;
46
+ const a = readLine ( ) . split ( ' ' ) . map ( Number ) ;
47
+
48
+ console . log ( modifyArray ( a ) . toString ( ) . split ( ',' ) . join ( ' ' ) ) ;
49
+ }
You can’t perform that action at this time.
0 commit comments