File tree 1 file changed +29
-0
lines changed
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ function caesarCipher ( str , num ) {
2
+ num = num % 26 ;
3
+ var lowerCaseString = str . toLowerCase ( ) ;
4
+ var alphabet = 'abcdefghijklmnopqrstuvwxyz' . split ( '' ) ;
5
+ var newString = '' ;
6
+
7
+ for ( var i = 0 ; i < lowerCaseString . length ; i ++ ) {
8
+ let currentLetter = lowerCaseString [ i ] ;
9
+ if ( currentLetter === ' ' ) {
10
+ newString += currentLetter ;
11
+ continue ;
12
+ }
13
+ let currentIndex = alphabet . indexOf ( currentLetter ) ;
14
+ let newIndex = currentIndex + num ;
15
+ if ( newIndex > 25 ) newIndex = newIndex - 26 ;
16
+ else if ( newIndex < 0 ) newIndex = 26 + newIndex ;
17
+ else newIndex = newIndex ;
18
+
19
+ if ( str [ i ] === str [ i ] . toUpperCase ( ) ) {
20
+ newString += alphabet [ newIndex ] . toUpperCase ( ) ;
21
+ } else newString += alphabet [ newIndex ] ;
22
+ }
23
+
24
+ console . log ( newString ) ;
25
+ return newString ;
26
+ }
27
+
28
+
29
+ caesarCipher ( 'Javascript' , - 900 ) ;
You can’t perform that action at this time.
0 commit comments