@@ -3,33 +3,55 @@ const display = document.getElementById("btn2");
3
3
const text = document . getElementById ( "text1" ) ;
4
4
const wrapper = document . getElementById ( "wrapper" ) ;
5
5
const item = document . getElementById ( "added-item" ) ;
6
+ const resetBtn = document . getElementById ( "btn3" ) ;
6
7
let result = document . getElementById ( "result" ) ;
8
+ display . setAttribute ( "disabled" , "" ) ;
7
9
8
10
let addedItem = 0 ;
9
11
let array = new Array ( ) ;
10
- add . addEventListener ( "click" , ( ) => {
11
- item . removeAttribute ( "style" ) ;
12
- if ( text . value == '' ) return
13
- else {
14
- array [ addedItem ] = text . value ;
15
- item . textContent = `You have added ${ array [ addedItem ] } to the array at index ${ addedItem } ` ;
16
- addedItem ++ ;
17
- console . log ( array ) ;
18
- text . value = "" ; }
19
- } ) ;
20
- display . addEventListener ( "click" , ( ) => {
12
+ let displayArray = ( ) => {
13
+ result . removeAttribute ( "style" ) ;
21
14
item . setAttribute ( "style" , "display:none;" ) ;
22
15
result . prepend ( "You have added the following to the array: " ) ;
23
- text . focus ( ) ;
24
- let e = '' ;
16
+ let e = "" ;
25
17
for ( let i = 0 ; i < array . length ; i ++ ) {
26
- e += `Element ${ i } is ${ array [ i ] } <br>` ;
18
+ e += `Element ${ i } is ${ array [ i ] } <br>` ;
27
19
result . innerHTML = e ;
28
- }
29
- } ) ;
30
- display . addEventListener ( "keypress" , ( ) => {
31
- if ( event . keyCode === 13 ) {
32
- display . click ( ) ;
20
+ }
21
+ text . focus ( ) ;
22
+ } ;
23
+ display . addEventListener ( "click" , displayArray ) ;
24
+
25
+ let addAnItemToArray = ( ) => {
26
+ item . removeAttribute ( "style" ) ;
27
+ display . removeAttribute ( "disabled" , "" ) ;
28
+ if ( text . value == "" ) return ;
29
+ else {
30
+ result . setAttribute ( "style" , "display:none;" ) ;
31
+ array [ addedItem ] = text . value ;
32
+ item . textContent = `You have added ${ array [ addedItem ] } to the array at index ${ addedItem } ` ;
33
+ addedItem ++ ;
34
+ console . log ( array ) ;
35
+ text . value = "" ;
36
+ }
37
+ text . focus ( ) ;
38
+ } ;
39
+
40
+ let resetArray = ( ) => {
41
+ array = [ ] ;
42
+ result . setAttribute ( "style" , "display:none;" ) ;
43
+ item . setAttribute ( "style" , "display:none;" ) ;
44
+ text . value == "" ;
45
+ display . setAttribute ( "disabled" , "" ) ;
46
+ text . focus ( ) ;
47
+ } ;
48
+ resetBtn . addEventListener ( "click" , resetArray ) ;
49
+ add . addEventListener ( "click" , addAnItemToArray ) ;
50
+
51
+ text . addEventListener ( "keydown" , function ( event ) {
52
+ if ( event . key === "Enter" ) {
53
+ event . preventDefault ( ) ;
54
+ addAnItemToArray ( ) ;
33
55
text . focus ( ) ;
34
56
}
35
57
} ) ;
0 commit comments