Problem with caesar cipher shift javascript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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 :
<form name="formu" action="">
<label for="pseudo">Your text :</label>
<br>
<textarea name="text" id="text_encode" style="width: 30%;height: 200px">
</textarea>
<br>
<br>
<label for="methods">Select your methods : </label>
<br>
<br>
<select name="methods" id="methods">
<option value="Caesar">Caesar</option>
</select>
<br>
<br>
<input type="button" value="Encrypt" onClick=encryption()>
<br>
<br>
<textarea name="text" id="text_decoded" style="width: 30%;height: 200px"
readonly="readonly"></textarea>
</form>
And here is my javascript code:
function encryption() {
switch(document.getElementById("methods").value) {
case "Caesar":
var str = document.getElementById("text_encode").value;
var amount = prompt("Number of shift");
var output = "";
for (var i = 0; i < str.length; i ++) {
var c = str[i];
var code = str.charCodeAt(i);
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
else if ( (code >= 97) && (code <= 122) )
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
output += c;
}
document.getElementById("text_decoded").value=output;
break;
}
}
You can also go here if you want to test what's wrong directly: https://www.w3schools.com/code/tryit.asp?filename=FXJU1NAG37C0
javascript jquery html string encryption
add a comment |
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 :
<form name="formu" action="">
<label for="pseudo">Your text :</label>
<br>
<textarea name="text" id="text_encode" style="width: 30%;height: 200px">
</textarea>
<br>
<br>
<label for="methods">Select your methods : </label>
<br>
<br>
<select name="methods" id="methods">
<option value="Caesar">Caesar</option>
</select>
<br>
<br>
<input type="button" value="Encrypt" onClick=encryption()>
<br>
<br>
<textarea name="text" id="text_decoded" style="width: 30%;height: 200px"
readonly="readonly"></textarea>
</form>
And here is my javascript code:
function encryption() {
switch(document.getElementById("methods").value) {
case "Caesar":
var str = document.getElementById("text_encode").value;
var amount = prompt("Number of shift");
var output = "";
for (var i = 0; i < str.length; i ++) {
var c = str[i];
var code = str.charCodeAt(i);
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
else if ( (code >= 97) && (code <= 122) )
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
output += c;
}
document.getElementById("text_decoded").value=output;
break;
}
}
You can also go here if you want to test what's wrong directly: https://www.w3schools.com/code/tryit.asp?filename=FXJU1NAG37C0
javascript jquery html string encryption
So what is the problem? Please edit your question. What happens when you run your code? What did you expect to happen instead?
– Robert
Nov 24 '18 at 21:34
add a comment |
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 :
<form name="formu" action="">
<label for="pseudo">Your text :</label>
<br>
<textarea name="text" id="text_encode" style="width: 30%;height: 200px">
</textarea>
<br>
<br>
<label for="methods">Select your methods : </label>
<br>
<br>
<select name="methods" id="methods">
<option value="Caesar">Caesar</option>
</select>
<br>
<br>
<input type="button" value="Encrypt" onClick=encryption()>
<br>
<br>
<textarea name="text" id="text_decoded" style="width: 30%;height: 200px"
readonly="readonly"></textarea>
</form>
And here is my javascript code:
function encryption() {
switch(document.getElementById("methods").value) {
case "Caesar":
var str = document.getElementById("text_encode").value;
var amount = prompt("Number of shift");
var output = "";
for (var i = 0; i < str.length; i ++) {
var c = str[i];
var code = str.charCodeAt(i);
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
else if ( (code >= 97) && (code <= 122) )
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
output += c;
}
document.getElementById("text_decoded").value=output;
break;
}
}
You can also go here if you want to test what's wrong directly: https://www.w3schools.com/code/tryit.asp?filename=FXJU1NAG37C0
javascript jquery html string encryption
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 :
<form name="formu" action="">
<label for="pseudo">Your text :</label>
<br>
<textarea name="text" id="text_encode" style="width: 30%;height: 200px">
</textarea>
<br>
<br>
<label for="methods">Select your methods : </label>
<br>
<br>
<select name="methods" id="methods">
<option value="Caesar">Caesar</option>
</select>
<br>
<br>
<input type="button" value="Encrypt" onClick=encryption()>
<br>
<br>
<textarea name="text" id="text_decoded" style="width: 30%;height: 200px"
readonly="readonly"></textarea>
</form>
And here is my javascript code:
function encryption() {
switch(document.getElementById("methods").value) {
case "Caesar":
var str = document.getElementById("text_encode").value;
var amount = prompt("Number of shift");
var output = "";
for (var i = 0; i < str.length; i ++) {
var c = str[i];
var code = str.charCodeAt(i);
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
else if ( (code >= 97) && (code <= 122) )
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
output += c;
}
document.getElementById("text_decoded").value=output;
break;
}
}
You can also go here if you want to test what's wrong directly: https://www.w3schools.com/code/tryit.asp?filename=FXJU1NAG37C0
javascript jquery html string encryption
javascript jquery html string encryption
edited Mar 8 at 1:25
Jack Bashford
17.8k51949
17.8k51949
asked Nov 24 '18 at 20:51
Hatu7Hatu7
93
93
So what is the problem? Please edit your question. What happens when you run your code? What did you expect to happen instead?
– Robert
Nov 24 '18 at 21:34
add a comment |
So what is the problem? Please edit your question. What happens when you run your code? What did you expect to happen instead?
– Robert
Nov 24 '18 at 21:34
So what is the problem? Please edit your question. What happens when you run your code? What did you expect to happen instead?
– Robert
Nov 24 '18 at 21:34
So what is the problem? Please edit your question. What happens when you run your code? What did you expect to happen instead?
– Robert
Nov 24 '18 at 21:34
add a comment |
1 Answer
1
active
oldest
votes
The mistake is this line:
var amount = prompt("Number of shift");
This by default returns a string, but it may contain a number - so if I enter 10
in the prompt box, amount
will be:
amount = "10"
To fix this, you need to parse the string into an integer:
var amount = Number(prompt("Number of shift"));
Then your code should work.
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462259%2fproblem-with-caesar-cipher-shift-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The mistake is this line:
var amount = prompt("Number of shift");
This by default returns a string, but it may contain a number - so if I enter 10
in the prompt box, amount
will be:
amount = "10"
To fix this, you need to parse the string into an integer:
var amount = Number(prompt("Number of shift"));
Then your code should work.
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
add a comment |
The mistake is this line:
var amount = prompt("Number of shift");
This by default returns a string, but it may contain a number - so if I enter 10
in the prompt box, amount
will be:
amount = "10"
To fix this, you need to parse the string into an integer:
var amount = Number(prompt("Number of shift"));
Then your code should work.
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
add a comment |
The mistake is this line:
var amount = prompt("Number of shift");
This by default returns a string, but it may contain a number - so if I enter 10
in the prompt box, amount
will be:
amount = "10"
To fix this, you need to parse the string into an integer:
var amount = Number(prompt("Number of shift"));
Then your code should work.
The mistake is this line:
var amount = prompt("Number of shift");
This by default returns a string, but it may contain a number - so if I enter 10
in the prompt box, amount
will be:
amount = "10"
To fix this, you need to parse the string into an integer:
var amount = Number(prompt("Number of shift"));
Then your code should work.
answered Nov 24 '18 at 21:04
Jack BashfordJack Bashford
17.8k51949
17.8k51949
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
add a comment |
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
Thanks! I thought that if I entered a number, It would correctly recognize even as a string, but I guess I'm going to need to be more careful next time.
– Hatu7
Nov 24 '18 at 21:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462259%2fproblem-with-caesar-cipher-shift-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So what is the problem? Please edit your question. What happens when you run your code? What did you expect to happen instead?
– Robert
Nov 24 '18 at 21:34