Skip to content
Advertisement

Problem with caesar cipher shift javascript

For a school project, I’m trying to create a website on encryption methods, but right now I have a problem with my Caesar one. I checked so many times but I can’t find out where is the problem in my code. I think the shift is what is wrong, but I don’t know what I could change to make it work, and I would be very happy if someone could help me.

So, here is the html part :

JavaScript

And here is my javascript code:

JavaScript

You can also go here if you want to test what’s wrong directly: https://www.w3schools.com/code/tryit.asp?filename=FXJU1NAG37C0

Advertisement

Answer

The mistake is this line:

JavaScript

This by default returns a string, but it may contain a number – so if I enter 10 in the prompt box, amount will be:

JavaScript

To fix this, you need to parse the string into an integer:

JavaScript

Then your code should work.

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