html - Caesar cipher in JavaScript returning unexpected results -
html - Caesar cipher in JavaScript returning unexpected results -
i creating webpage calculate simple caesar cipher without using jquery. cannot locate error , not sure of how homecoming new string text area.
html:
<input type="button" value="encrypt value = 1" onclick ="caesarencipher(shift, text)"/>
javascript:
function caesarencipher(shift, plaintext) { this.shift = shift; this.plaintext = plaintext; var ciphertext (var = 0; < plaintext.length; i++) { // ascii value - numerical representation // 65 = 'a' 90 = 'z' var encode = plaintext.charcodeat(i); if (encode >= 65 && encode <= 90) // uppercase ciphertext += string.fromcharcode((encode - 65 + shift) % 26 + 65); // 97 = 'a' 122 = 'z' else if (encode >= 97 && encode <= 122) // lowercase ciphertext += string.fromcharcode((encode - 97 + shift) % 26 + 97); else ciphertext += input.charat(i); } homecoming document.getelementbyid = ciphertext; <-- not sure }
http://jsfiddle.net/y9rv6bux/
class="snippet-code-js lang-js prettyprint-override">function encrypt(id, shiftid) { var t = document.getelementbyid(id), out = ''; var shift = parseint(document.getelementbyid(shiftid).value); var txt = t.value, ranges = [[65,90],[97,122]]; for(var = 0; < txt.length; i++) { var code = txt.charcodeat(i); for(var j = 0; j < ranges.length; j++) { if (code >= ranges[j][0] && code <= ranges[j][1]) { code = ((code - ranges[j][0] + shift) % (ranges[j][1] - ranges[j][0] + 1)) + ranges[j][0]; break; } } out += string.fromcharcode(code); } t.value = out; }
class="snippet-code-html lang-html prettyprint-override"><textarea id='t'></textarea><br><input type='text' id='s' value='1'><br> <input type='button' onclick='encrypt("t", "s")' value='go'>
javascript html function encode
Comments
Post a Comment