Setting Up A For Loop For An Encryption Scheme
As I stated before I am trying to make a specific for loop for an encryption machine (a toy machine, not one that is secure from the NSA). When I press the encrypt button, I need t
Solution 1:
Here's an interpretation of what I think you're trying to do:
functionencrypt(num) {
var sum = 0, str, i, result, index;
var chars = "abcdefghijklmnop";
var charBase = "0".charCodeAt(0);
for (i = 0; i < 5; i++) {
// ((2 * i) + 3) goes 3,5,7,9,11// ((i * 10) + 10) goes 10,20,30,40,50
sum += (num * ((2 * i) + 3)) + ((i * 10) + 10);
}
// convert num to string to get digits
str = sum + "";
result = "";
for (i = 0; i < str.length; i++) {
// get offset from "0" and add 3 for the cipher
index = str.charCodeAt(i) - charBase + 3;
// convert that index to a character
result += chars.charAt(index);
}
return result;
}
It takes a number as an argument and it returns a string.
Working demo: http://jsfiddle.net/jfriend00/UW245/
Post a Comment for "Setting Up A For Loop For An Encryption Scheme"