File tree Expand file tree Collapse file tree 1 file changed +7
-11
lines changed
challenges/easy/caesar-cipher Expand file tree Collapse file tree 1 file changed +7
-11
lines changed Original file line number Diff line number Diff line change 11function encode ( word : string , shift : number ) {
22
33 shift = shift % 26 ;
4- let ascii : number ;
5- let new_code : number ;
64
75 return word . split ( "" ) . map ( caesar => {
8- new_code = 0 ;
9- ascii = caesar . charCodeAt ( 0 ) ;
6+ const ascii : number = caesar . charCodeAt ( 0 ) ;
7+ const new_code : number = ascii + shift ;
108 if ( ascii >= 65 && ascii <= 90 ) {
11- new_code = ascii + shift ;
12- if ( new_code < 65 ) { new_code += 26 }
13- if ( new_code > 90 ) { new_code -= 26 }
14- return String . fromCharCode ( new_code )
9+ if ( new_code < 65 ) { return String . fromCharCode ( new_code + 26 ) }
10+ if ( new_code > 90 ) { return String . fromCharCode ( new_code - 26 ) }
11+ else return String . fromCharCode ( new_code )
1512 } else if ( ascii >= 97 && ascii <= 122 ) {
16- new_code = ascii + shift ;
17- if ( new_code < 97 ) { new_code += 26 }
18- if ( new_code > 122 ) { new_code -= 26 }
13+ if ( new_code < 97 ) { return String . fromCharCode ( new_code + 26 ) }
14+ if ( new_code > 122 ) { return String . fromCharCode ( new_code - 26 ) }
1915 return String . fromCharCode ( new_code )
2016 } else return caesar ;
2117 } ) . join ( '' ) ;
You can’t perform that action at this time.
0 commit comments