Skip to content
Advertisement

How can i convert the index of a string that is marked as a string in an integer number?

I am trying to code a Caesar cipher and i am trying to make a loop around the alphabet so that even if a put a high number as a shifter it doesn’t give me an error. The problem is it tells me i can’t compare a string with a number, so when i put the new index like this “int(new_index)” i still get an error. The teacher of the course i am following said to just copy and paste the alphabet list twice but what if i put 1000 as a shifter?

JavaScript

My idea was to keep subtracting 25 from the new_index with a while loop when the index is higher then 25, so that even if i put 10000 as the shifter i don’t have to copy the list hundreds of time. What am i doing wrong? Thank you all in advance!

Advertisement

Answer

set new_index = index + shifter then after the while statement put encrypted_message += alphabet[new_index]

the full else statement:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement