1+ document . getElementById ( 'cipher-submit' ) . addEventListener ( 'click' , cipher ) ;
2+ document . getElementById ( 'cipher-switch' ) . addEventListener ( 'click' , cipherSwitch ) ;
3+
4+ var cipherState = 'encrypt' ;
5+
6+ function cipherSwitch ( ) {
7+ let cipherSubmitInfo = document . getElementById ( 'cipher-submit' ) ;
8+ if ( cipherState == 'encrypt' ) {
9+ cipherState = 'decrypt' ;
10+ cipherSubmitInfo . textContent = 'Decrypt!' ;
11+ console . log ( 'Now decrypting' ) ;
12+ return ;
13+ } else {
14+ cipherState = 'encrypt' ;
15+ cipherSubmitInfo . textContent = 'Encrypt!' ;
16+ console . log ( 'Now encrypting' ) ;
17+ return ;
18+ } ;
19+ } ;
20+
21+
22+
23+ function cipher ( ) {
24+
25+ var cipherError = 'Unknown error!' ;
26+
27+ let cipherOutput = document . getElementById ( 'cipher-output' ) ;
28+ let cipherInput = document . getElementById ( 'cipher-input' ) . value ;
29+ let cipherInputBox = document . getElementById ( 'cipher-input' ) ;
30+ let cipherNumber = parseInt ( document . getElementById ( 'cipher-number' ) . value , 10 ) ;
31+ let cipherNumberBox = document . getElementById ( 'cipher-number' ) ;
32+
33+ function showError ( ) {
34+ let newError = document . createElement ( 'div' ) ;
35+ newError . classList = 'd-block my-alert absolute-top alert alert-danger' ;
36+
37+ let closeButton = document . createElement ( 'a' ) ;
38+ closeButton . setAttribute ( 'href' , '#' ) ;
39+ closeButton . classList = 'close' ;
40+ closeButton . setAttribute ( 'data-dismiss' , 'alert' ) ;
41+ closeButton . setAttribute ( 'aria-label' , 'Close' ) ;
42+ closeButton . innerHTML = '×' ;
43+ newError . appendChild ( closeButton ) ;
44+
45+ let errorText1 = document . createElement ( 'strong' ) ;
46+ errorText1 . textContent = 'Warning! ' ;
47+ newError . appendChild ( errorText1 )
48+
49+ let errorText2 = document . createElement ( 'span' ) ;
50+ errorText2 . textContent = cipherError ;
51+ newError . appendChild ( errorText2 ) ;
52+
53+ document . getElementById ( 'alerts' ) . appendChild ( newError ) ;
54+ window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
55+ } ;
56+
57+ let alfabeth = [
58+ 'A' ,
59+ 'B' ,
60+ 'C' ,
61+ 'D' ,
62+ 'E' ,
63+ 'F' ,
64+ 'G' ,
65+ 'H' ,
66+ 'I' ,
67+ 'J' ,
68+ 'K' ,
69+ 'L' ,
70+ 'M' ,
71+ 'N' ,
72+ 'O' ,
73+ 'P' ,
74+ 'Q' ,
75+ 'R' ,
76+ 'S' ,
77+ 'T' ,
78+ 'U' ,
79+ 'V' ,
80+ 'W' ,
81+ 'X' ,
82+ 'Y' ,
83+ 'Z'
84+ ] ;
85+
86+ cipherInputBox . classList . remove ( 'wrong' ) ;
87+ cipherNumberBox . classList . remove ( 'wrong' ) ;
88+
89+ // is empty
90+ if ( ( cipherInput == '' ) || ( cipherInput == ' ' ) ) {
91+ cipherInputBox . classList . add ( 'wrong' ) ;
92+ cipherError = 'Text area can\'t be empty!' ;
93+ showError ( ) ;
94+ return ;
95+ } ;
96+
97+ // starts with space
98+ if ( ( cipherInput . charAt ( 0 ) == ' ' ) ) {
99+ cipherInputBox . classList . add ( 'wrong' ) ;
100+ cipherError = 'Text area can\'t start with space!' ;
101+ showError ( ) ;
102+ return ;
103+ }
104+
105+ cipherInput = cipherInput . toUpperCase ( ) ;
106+
107+ // contains not specified characters
108+ for ( let i = 0 ; i < cipherInput . length ; i ++ ) {
109+ if ( cipherInput . charAt ( i ) == ' ' ) {
110+ continue ;
111+ } else if ( ! ( alfabeth . includes ( cipherInput . charAt ( i ) ) ) ) {
112+ cipherInputBox . classList . add ( 'wrong' ) ;
113+ cipherError = 'Text area contains unexpected characters!' ;
114+ showError ( ) ;
115+ return ;
116+ } ;
117+ } ;
118+
119+ // end with space
120+ if ( cipherInput . charAt ( ( cipherInput . length - 1 ) ) == ' ' ) {
121+ cipherInputBox . classList . add ( 'wrong' ) ;
122+ cipherError = 'Text area can\'t end with space!' ;
123+ showError ( ) ;
124+ return ;
125+ } ;
126+
127+ // does not containt number
128+ if ( isNaN ( cipherNumber ) ) {
129+ cipherNumberBox . classList . add ( 'wrong' ) ;
130+ cipherError = 'No number provided!' ;
131+ showError ( ) ;
132+ return ;
133+ } ;
134+
135+ // number out of range
136+ if ( ( cipherNumber < 1 ) || ( cipherNumber > 26 ) ) {
137+ cipherNumberBox . classList . add ( 'wrong' ) ;
138+ cipherError = 'Number is out of specified index!' ;
139+ showError ( ) ;
140+ return ;
141+ } ;
142+
143+ console . log ( 'Processing text - ' + cipherInput ) ;
144+ console . log ( 'With number - ' + cipherNumber ) ;
145+ console . log ( 'Type - ' + cipherState ) ;
146+
147+ // decrypt
148+ if ( cipherState == 'decrypt' ) {
149+ cipherNumber = 26 - cipherNumber ;
150+ } ;
151+
152+ let cipherAlfabeth = alfabeth . slice ( ) ;
153+
154+ let temp ;
155+ for ( let i = 1 ; i <= cipherNumber ; i ++ ) {
156+ temp = cipherAlfabeth [ 0 ] ;
157+ cipherAlfabeth . splice ( 0 , 1 ) ;
158+ cipherAlfabeth . push ( temp ) ;
159+ } ;
160+ temp = '' ;
161+
162+ let cipherOutputText = '' ;
163+ let cipherCharIndex = 0 ;
164+ for ( let i = 0 ; i < cipherInput . length ; i ++ ) {
165+ if ( cipherInput . charAt ( i ) == ' ' ) {
166+ cipherOutputText += ' ' ;
167+ } else {
168+ cipherCharIndex = alfabeth . indexOf ( cipherInput . charAt ( i ) ) ;
169+ cipherOutputText += cipherAlfabeth [ cipherCharIndex ] ;
170+ } ;
171+ } ;
172+ cipherOutput . textContent = cipherOutputText ;
173+ window . scrollTo ( { top : document . body . scrollHeight , behavior : 'smooth' } ) ;
174+ } ;
0 commit comments