File tree Expand file tree Collapse file tree 5 files changed +146
-0
lines changed
05-while-loop/3-more-exercises Expand file tree Collapse file tree 5 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
1
+ function dishwasher ( input ) {
2
+ let detergentBottles = parseInt ( input [ 0 ] ) ;
3
+
4
+ let totalDetergent = detergentBottles * 750 ;
5
+
6
+ let i = 1 ;
7
+ let cycle = 1 ;
8
+ let totalDishes = 0 ;
9
+ let totalPots = 0 ;
10
+
11
+ while ( input [ i ] != 'End' ) {
12
+ let items = parseInt ( input [ i ] ) ;
13
+ let neededDetergent ;
14
+
15
+ if ( cycle % 3 == 0 ) {
16
+ neededDetergent = items * 15 ;
17
+ totalPots += items ;
18
+ } else {
19
+ neededDetergent = items * 5 ;
20
+ totalDishes += items ;
21
+ }
22
+
23
+ if ( totalDetergent >= neededDetergent ) {
24
+ totalDetergent -= neededDetergent ;
25
+ } else {
26
+ console . log ( `Not enough detergent, ${ neededDetergent - totalDetergent } ml. more necessary!` ) ;
27
+ return ;
28
+ }
29
+
30
+ i ++ ;
31
+ cycle ++ ;
32
+ }
33
+
34
+ console . log ( "Detergent was enough!" ) ;
35
+ console . log ( `${ totalDishes } dishes and ${ totalPots } pots were washed.` ) ;
36
+ console . log ( `Leftover detergent ${ totalDetergent } ml.` ) ;
37
+ }
Original file line number Diff line number Diff line change
1
+ function reportSystem ( input ) {
2
+ let targetAmount = parseInt ( input [ 0 ] ) ;
3
+ let collectedCashAmount = 0 ;
4
+ let collectedCardAmount = 0 ;
5
+ let cashTransactionsCount = 0 ;
6
+ let cardTransactionsCount = 0 ;
7
+ let totalCount = 0 ;
8
+ let i = 1 ;
9
+
10
+ while ( true ) {
11
+ if ( input [ i ] === "End" ) {
12
+ console . log ( "Failed to collect required money for charity." ) ;
13
+ return ;
14
+ }
15
+
16
+ let price = parseInt ( input [ i ] ) ;
17
+
18
+ totalCount ++ ;
19
+
20
+ if ( totalCount % 2 !== 0 ) { // Cash payment
21
+ if ( price > 100 ) {
22
+ console . log ( "Error in transaction!" ) ;
23
+ } else {
24
+ console . log ( "Product sold!" ) ;
25
+ collectedCashAmount += price ;
26
+ cashTransactionsCount ++ ;
27
+ }
28
+ } else { // Card payment
29
+ if ( price < 10 ) {
30
+ console . log ( "Error in transaction!" ) ;
31
+ } else {
32
+ console . log ( "Product sold!" ) ;
33
+ collectedCardAmount += price ;
34
+ cardTransactionsCount ++ ;
35
+ }
36
+ }
37
+
38
+ if ( collectedCashAmount + collectedCardAmount >= targetAmount ) {
39
+ break ;
40
+ }
41
+
42
+ i ++ ;
43
+ }
44
+
45
+ let averageCash = collectedCashAmount / cashTransactionsCount ;
46
+ let averageCard = collectedCardAmount / cardTransactionsCount ;
47
+
48
+ console . log ( `Average CS: ${ averageCash . toFixed ( 2 ) } ` ) ;
49
+ console . log ( `Average CC: ${ averageCard . toFixed ( 2 ) } ` ) ;
50
+ }
Original file line number Diff line number Diff line change
1
+ function streamOfLetters ( input ) {
2
+ let currentWord = '' ;
3
+ let result = '' ;
4
+
5
+ let foundC = false , foundO = false , foundN = false ;
6
+
7
+ for ( let i = 0 ; i < input . length ; i ++ ) {
8
+ if ( input [ i ] === "End" ) {
9
+ break ;
10
+ }
11
+
12
+ let ch = input [ i ] ;
13
+ if ( ! / [ a - z A - Z ] / . test ( ch ) ) {
14
+ continue ;
15
+ }
16
+
17
+ if ( ch === 'c' || ch === 'o' || ch === 'n' ) {
18
+ if ( ch === 'c' && ! foundC ) {
19
+ foundC = true ;
20
+ } else if ( ch === 'o' && ! foundO ) {
21
+ foundO = true ;
22
+ } else if ( ch === 'n' && ! foundN ) {
23
+ foundN = true ;
24
+ } else {
25
+ currentWord += ch ;
26
+ }
27
+
28
+ if ( foundC && foundO && foundN ) {
29
+ result += currentWord + ' ' ;
30
+ currentWord = '' ;
31
+ foundC = foundO = foundN = false ;
32
+ }
33
+ } else {
34
+ currentWord += ch ;
35
+ }
36
+ }
37
+
38
+ console . log ( result . trim ( ) ) ;
39
+ }
Original file line number Diff line number Diff line change
1
+ function numbersDividedBy3WithoutRemainder ( ) {
2
+ for ( let i = 1 ; i <= 100 ; i ++ ) {
3
+ if ( i % 3 === 0 ) {
4
+ console . log ( i ) ;
5
+ }
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ function averageNumber ( input ) {
2
+ let n = parseInt ( input [ 0 ] ) ;
3
+ let sum = 0 ;
4
+
5
+ for ( let i = 1 ; i <= n ; i ++ ) {
6
+ let number = parseInt ( input [ i ] ) ;
7
+ sum += number ;
8
+ }
9
+
10
+ let average = sum / n ;
11
+
12
+ console . log ( average . toFixed ( 2 ) ) ;
13
+ }
You can’t perform that action at this time.
0 commit comments