AS3 Cipher Decoder & Encoder (better answer?) - actionscript-3

Last night, my little brother asked me if it were possible to make a program that would substitute every letter for another letter in the alphabet, to turn it into a code, and also turn it back to its normal state.
So I made this: http://www.skyetheguy.com/novatranslationtool
And by all accounts, it does the job, but I feel like, code-wise, it's clunky as all get-out, and I'd really love to improve my knowledge on things like loops...
EDIT: The above has since been updated with DodgerThud's amazing answer and works like a dream. : ) Dreams DO come true!
Also, this is very pie-in-the-sky, but it'd be amazing to get this to work with longer-than-two-characters things. My code definitely wouldn't do that at all. But, you know, all those fun codes with apostrophes in them... good stuff!
stop();
// SET-UP STUFF
inputText.alpha = .5;
outputText.alpha = .5;
inputText.borderColor = 0xCCCCCC;
outputText.borderColor = 0xCCCCCC;
var textinProgress:String;
textinProgress = inputText.text;
if (textinProgress.charAt(0) == "\r") {
inputText.text = "";
}
stage.addEventListener(Event.ENTER_FRAME, refresh_frame1);
function refresh_frame1(e:Event):void {
inputScroll.update();
outputScroll.update();
}
// ORIGINAL ALPHABET
var letters_aArray:Array = new Array();
letters_aArray[0] = "a";
letters_aArray[1] = "b";
letters_aArray[2] = "c";
letters_aArray[3] = "d";
letters_aArray[4] = "e";
letters_aArray[5] = "f";
letters_aArray[6] = "g";
letters_aArray[7] = "h";
letters_aArray[8] = "i";
letters_aArray[9] = "j";
letters_aArray[10] = "k";
letters_aArray[11] = "l";
letters_aArray[12] = "m";
letters_aArray[13] = "n";
letters_aArray[14] = "o";
letters_aArray[15] = "p";
letters_aArray[16] = "q";
letters_aArray[17] = "r";
letters_aArray[18] = "s";
letters_aArray[19] = "t";
letters_aArray[20] = "u";
letters_aArray[21] = "v";
letters_aArray[22] = "w";
letters_aArray[23] = "x";
letters_aArray[24] = "y";
letters_aArray[25] = "z";
// NEW ALPHABET
var letters_bArray:Array = new Array();
letters_bArray[0] = "m";
letters_bArray[1] = "n";
letters_bArray[2] = "b";
letters_bArray[3] = "v";
letters_bArray[4] = "c";
letters_bArray[5] = "x";
letters_bArray[6] = "z";
letters_bArray[7] = "l";
letters_bArray[8] = "k";
letters_bArray[9] = "j";
letters_bArray[10] = "h";
letters_bArray[11] = "g";
letters_bArray[12] = "f";
letters_bArray[13] = "d";
letters_bArray[14] = "s";
letters_bArray[15] = "a";
letters_bArray[16] = "p";
letters_bArray[17] = "o";
letters_bArray[18] = "i";
letters_bArray[19] = "u";
letters_bArray[20] = "y";
letters_bArray[21] = "t";
letters_bArray[22] = "r";
letters_bArray[23] = "e";
letters_bArray[24] = "w";
letters_bArray[25] = "q";
// ENCODE BUTTON
encode_btn.addEventListener(MouseEvent.CLICK, encode_btnclick);
function encode_btnclick(event:MouseEvent):void
{
textinProgress = inputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_aArray[0]) == 0) {
outputText.appendText(letters_bArray[0]);
} else if (textinProgress.charAt(b).search(letters_aArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[1]) == 0) {
outputText.appendText(letters_bArray[1]);
} else if (textinProgress.charAt(b).search(letters_aArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[2]) == 0) {
outputText.appendText(letters_bArray[2]);
} else if (textinProgress.charAt(b).search(letters_aArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[3]) == 0) {
outputText.appendText(letters_bArray[3]);
} else if (textinProgress.charAt(b).search(letters_aArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[4]) == 0) {
outputText.appendText(letters_bArray[4]);
} else if (textinProgress.charAt(b).search(letters_aArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[5]) == 0) {
outputText.appendText(letters_bArray[5]);
} else if (textinProgress.charAt(b).search(letters_aArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[6]) == 0) {
outputText.appendText(letters_bArray[6]);
} else if (textinProgress.charAt(b).search(letters_aArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[7]) == 0) {
outputText.appendText(letters_bArray[7]);
} else if (textinProgress.charAt(b).search(letters_aArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[8]) == 0) {
outputText.appendText(letters_bArray[8]);
} else if (textinProgress.charAt(b).search(letters_aArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[9]) == 0) {
outputText.appendText(letters_bArray[9]);
} else if (textinProgress.charAt(b).search(letters_aArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[10]) == 0) {
outputText.appendText(letters_bArray[10]);
} else if (textinProgress.charAt(b).search(letters_aArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[11]) == 0) {
outputText.appendText(letters_bArray[11]);
} else if (textinProgress.charAt(b).search(letters_aArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[12]) == 0) {
outputText.appendText(letters_bArray[12]);
} else if (textinProgress.charAt(b).search(letters_aArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[13]) == 0) {
outputText.appendText(letters_bArray[13]);
} else if (textinProgress.charAt(b).search(letters_aArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[14]) == 0) {
outputText.appendText(letters_bArray[14]);
} else if (textinProgress.charAt(b).search(letters_aArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[15]) == 0) {
outputText.appendText(letters_bArray[15]);
} else if (textinProgress.charAt(b).search(letters_aArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[16]) == 0) {
outputText.appendText(letters_bArray[16]);
} else if (textinProgress.charAt(b).search(letters_aArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[17]) == 0) {
outputText.appendText(letters_bArray[17]);
} else if (textinProgress.charAt(b).search(letters_aArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[18]) == 0) {
outputText.appendText(letters_bArray[18]);
} else if (textinProgress.charAt(b).search(letters_aArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[19]) == 0) {
outputText.appendText(letters_bArray[19]);
} else if (textinProgress.charAt(b).search(letters_aArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[20]) == 0) {
outputText.appendText(letters_bArray[20]);
} else if (textinProgress.charAt(b).search(letters_aArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[21]) == 0) {
outputText.appendText(letters_bArray[21]);
} else if (textinProgress.charAt(b).search(letters_aArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[22]) == 0) {
outputText.appendText(letters_bArray[22]);
} else if (textinProgress.charAt(b).search(letters_aArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[23]) == 0) {
outputText.appendText(letters_bArray[23]);
} else if (textinProgress.charAt(b).search(letters_aArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[24]) == 0) {
outputText.appendText(letters_bArray[24]);
} else if (textinProgress.charAt(b).search(letters_aArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[25]) == 0) {
outputText.appendText(letters_bArray[25]);
} else if (textinProgress.charAt(b).search(letters_aArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
// DECODE BUTTON
decode_btn.addEventListener(MouseEvent.CLICK, decode_btnclick);
function decode_btnclick(event:MouseEvent):void
{
textinProgress = inputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
outputText.appendText(letters_aArray[0]);
} else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
outputText.appendText(letters_aArray[1]);
} else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
outputText.appendText(letters_aArray[2]);
} else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
outputText.appendText(letters_aArray[3]);
} else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
outputText.appendText(letters_aArray[4]);
} else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
outputText.appendText(letters_aArray[5]);
} else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
outputText.appendText(letters_aArray[6]);
} else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
outputText.appendText(letters_aArray[7]);
} else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
outputText.appendText(letters_aArray[8]);
} else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
outputText.appendText(letters_aArray[9]);
} else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
outputText.appendText(letters_aArray[10]);
} else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
outputText.appendText(letters_aArray[11]);
} else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
outputText.appendText(letters_aArray[12]);
} else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
outputText.appendText(letters_aArray[13]);
} else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
outputText.appendText(letters_aArray[14]);
} else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
outputText.appendText(letters_aArray[15]);
} else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
outputText.appendText(letters_aArray[16]);
} else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
outputText.appendText(letters_aArray[17]);
} else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
outputText.appendText(letters_aArray[18]);
} else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
outputText.appendText(letters_aArray[19]);
} else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
outputText.appendText(letters_aArray[20]);
} else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
outputText.appendText(letters_aArray[21]);
} else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
outputText.appendText(letters_aArray[22]);
} else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
outputText.appendText(letters_aArray[23]);
} else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
outputText.appendText(letters_aArray[24]);
} else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
outputText.appendText(letters_aArray[25]);
} else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
// RECODE BUTTON (scrambles, takes what's in the output and applies the cipher again)
recode_btn.addEventListener(MouseEvent.CLICK, recode_btnclick);
function recode_btnclick(event:MouseEvent):void
{
textinProgress = outputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
outputText.appendText(letters_aArray[0]);
} else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
outputText.appendText(letters_aArray[1]);
} else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
outputText.appendText(letters_aArray[2]);
} else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
outputText.appendText(letters_aArray[3]);
} else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
outputText.appendText(letters_aArray[4]);
} else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
outputText.appendText(letters_aArray[5]);
} else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
outputText.appendText(letters_aArray[6]);
} else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
outputText.appendText(letters_aArray[7]);
} else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
outputText.appendText(letters_aArray[8]);
} else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
outputText.appendText(letters_aArray[9]);
} else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
outputText.appendText(letters_aArray[10]);
} else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
outputText.appendText(letters_aArray[11]);
} else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
outputText.appendText(letters_aArray[12]);
} else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
outputText.appendText(letters_aArray[13]);
} else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
outputText.appendText(letters_aArray[14]);
} else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
outputText.appendText(letters_aArray[15]);
} else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
outputText.appendText(letters_aArray[16]);
} else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
outputText.appendText(letters_aArray[17]);
} else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
outputText.appendText(letters_aArray[18]);
} else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
outputText.appendText(letters_aArray[19]);
} else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
outputText.appendText(letters_aArray[20]);
} else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
outputText.appendText(letters_aArray[21]);
} else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
outputText.appendText(letters_aArray[22]);
} else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
outputText.appendText(letters_aArray[23]);
} else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
outputText.appendText(letters_aArray[24]);
} else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
outputText.appendText(letters_aArray[25]);
} else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
Just gonna tell you why I did all the incredibly arbitrary and loopy stuff that I did...
If you do any of the other normal replace things other people have talked about here (from what I've FOUND, at least), you end up replacing your own work at a certain point. For example if you want B to be A, but then you want A to be C, then if you type "BA", you get back "CC" instead of "AC". So my code actually takes each character and spits them all out one at a time.
I did all that tedious ".toUpperCase()" stuff to make sure that this tool was case-sensitive-- so that if I typed in a lowercase or uppercase key, it would always generate the proper one in its place. That's why RegExp didn't quite work for me.
It also takes into account things like line breaks and random symbols not included in the alphabet.
I know for SURE there's simpler, more energy-efficient ways to code this, but I just have no idea how.

I'm gonna expand on my comment and show you how can simplify your code and cut out a lot of it.
First off, you don't need to use Arrays. String objects in essence already are Arrays of byte values. So instead of creating big arrays, you can do this
var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewq";
In order to access a specific character, you can simply call the indexOf method on the string.
var indexOfJ = alphabet.indexOf("j");
trace(indexOfJ); //puts out 9
And you already now how to get the character of a string from a specific index.
var charAtIndex7:String = alphabet.charAt(7);
trace(charAtIndex7); //puts out "h"
Edit fom here:
Looks like I was only half-awake when I wrote that answer last night. The issue with the double for-loop is simply that it will always run the whole way through, even if it found an if clause that gets accepted. You already figured out on
your own that the last else-clause causes the input character to be printed 25 times. So, lets fix it.
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var anyHit = false;
for(var i=0;i<alphabet.length;i++){
if(textinProgress.charAt(b).search(alphabet.charAt(i)) == 0){
result += substituton.charAt(i);
anyHit = true;
break;
}
else if(textinProgress.charAt(b).search(alphabet.charAt(i).toUpperCase()) == 0){
result += substituton.charAt(i).toUpperCase();
anyHit = true;
break;
}
else if(textinProgress.charAt(b) == "\r"){
result += "\n";
anyHit = true;
break;
}
}
if(anyHit == false){
result += textinProgress.charAt(b);
}
}
output.text = result;
}
I had a coworker test this code for me earlier today, and it should work as intended.
But that's not all we can do to simplify your code. We can cut out the second if clause by defining our alphabet and substitution variables with both lowercase and uppercase letters.
var alphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
You should also be able to put in special symbols into your alphabet and substitution string, like \r for example.
var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
I'm not entirely sure if this will work, but it should.
Now you have already considerably shortened your code, but we can even shorten it further. Is the inner for loop really neccessary? Because our alphabet and our substitution are the same length, maybe we can simplify this even more.
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var index:int = alphabet.indexOf(textinProgress.charAt(b));
if(index >= 0){
result += substitution.charAt(index);
}else{
result += textinProgress.charAt(b);
}
}
output.text = result;
}
Putting it all together.
var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var index:int = alphabet.indexOf(textinProgress.charAt(b));
if(index >= 0){
result += substitution.charAt(index);
}else{
result += textinProgress.charAt(b);
}
}
output.text = result;
}
function encode_btnclick(event:MouseEvent):void{
cipher(inputText, outputText, alphabet, substitution);
}
function decode_btnclick(event:MouseEvent):void{
cipher(inputText, outputText, substitution, alphabet);
}
function recode_btnclick(event:MouseEvent):void{
cipher(outputText, outputText, alphabet, substitution);
}
This should be able to handle
Text of any length
Line breaks
Upper- and Lowercase letters
Undefined characters

You should search for Substitution ciphers, e.g., one of the simplest ones is the Caesar cipher.

Related

Encountering an error while attempting to create a game

I'm trying to create a crossword puzzle game using Actionscript 3.0 in Adobe
Animate. For the game coding, I followed an online tutorial and now I'm encountering an error. The error occurs whenever I'm in the actual game scene, no errors are encountered on the home page or instruction page. can anyone help me? my coding skills are below basic.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at CROSSWORD_CODE_fla::MainTimeline/entFrame().
stop();
quitbtn.addEventListener(MouseEvent.CLICK, quitt);
function quitt(evt: MouseEvent): void {
gotoAndStop("start");
}
var hippopotamus: Number = 0;
var horse: Number = 0;
var eel: Number = 0;
var frog: Number = 0;
var ant: Number = 0;
var turtle: Number = 0;
var tiger: Number = 0;
var bird: Number = 0;
var penguin: Number = 0;
var orca: Number = 0;
var octopus: Number = 0;
var owl: Number = 0;
var snail: Number = 0;
var duck: Number = 0;
var kangaroo: Number = 0;
var iguana: Number = 0;
var ALLwords:Number = 0;
var Total:Number = 0;
{
stage.addEventListener(Event.ENTER_FRAME, entFrame);
function entFrame(evt:Event):void {
if ((hypoH.text == ("h").toString())
&& (hypo1.text == ("i").toString())
&& (hypoP.text == ("p").toString())
&& (hypoP2.text == ("p").toString())
&& (hypoO.text == ("o").toString())
&& (hypoP3.text == ("p").toString())
&& (hypoO2.text == ("o").toString())
&& (hypoT.text == ("t").toString())
&& (hypoA.text == ("a").toString())
&& (hypoM.text == ("m").toString())
&& (hypoU.text == ("u").toString())
&& (hypoS.text == ("s").toString()))
{
hippopotamus = 1;
trace("hippopotamus");
}
}
{
if ((hrsO.text == ("o").toString())
&& (hrsR.text == ("r").toString())
&& (hrsS.text == ("s").toString())
&& (hrsE.text == ("e").toString()))
{
horse = 1;
trace("horse");
}
}
{
if ((eelE.text == ("e").toString())
&& (eel1.text == ("l").toString()))
{
eel = 1;
trace("eel");
}
}
{
if ((penP.text == ("p").toString())
&& (penE.text == ("e").toString())
&& (penN.text == ("n").toString())
&& (penG.text == ("g").toString())
&& (penU.text == ("u").toString())
&& (pen1.text == ("i").toString())
&& (penN2.text == ("n").toString()))
{
penguin = 1;
trace("penguin");
}
}
{
if ((brdB.text == ("b").toString())
&& (brdR.text == ("r").toString())
&& (brdD.text == ("d").toString()))
{
bird = 1;
trace("bird");
}
}
{
if ((octO.text == ("o").toString())
&& (octC.text == ("c").toString())
&& (octT.text == ("t").toString())
&& (octO2.text == ("0").toString())
&& (octP.text == ("p").toString())
&& (octU.text == ("u").toString()))
{
octopus = 1;
trace("octopus");
}
}
{
if ((orcR.text == ("r").toString())
&& (orcC.text == ("c").toString())
&& (orcA.text == ("a").toString()))
{
orca = 1;
trace("orca");
}
}
{
if ((owlO.text == ("o").toString())
&& (owlW.text == ("w").toString()))
{
owl = 1;
trace("owl");
}
}
{
if ((nailS.text == ("s").toString())
&& (nailA.text == ("a").toString())
&& (nail1.text == ("i").toString())
&& (nailL.text == ("l").toString()))
{
snail = 1;
trace("snail");
}
}
{
if ((dkD.text == ("d").toString())
&& (dkU.text == ("u").toString())
&& (dkC.text == ("c").toString())
&& (dkK.text == ("k").toString()))
{
duck = 1;
trace("duck");
}
}
{
if ((groA.text == ("a").toString())
&& (groN.text == ("n").toString())
&& (groG.text == ("g").toString())
&& (groA2.text == ("a").toString())
&& (groR.text == ("r").toString())
&& (groO.text == ("o").toString()))
{
kangaroo = 1;
trace("kangaroo");
}
}
{
if ((frgF.text == ("f").toString())
&& (frgR.text == ("r").toString())
&& (frgG.text == ("g").toString()))
{
frog = 1;
trace("frog");
}
}
{
if ((ignG.text == ("g").toString())
&& (ignU.text == ("u").toString())
&& (ignA.text == ("a").toString())
&& (ignN.text == ("n").toString()))
{
iguana = 1;
trace("iguana");
}
}
{
if ((antA.text == ("a").toString())
&& (antN.text == ("n").toString()))
{
ant = 1;
trace("ant");
}
}
{
if ((trtT.text == ("t").toString())
&& (trtU.text == ("u").toString())
&& (trtR.text == ("r").toString())
&& (trtT2.text == ("t").toString())
&& (trtL.text == ("l").toString()))
{
turtle = 1;
trace("turtle");
}
}
{
if ((tgr1.text == ("i").toString())
&& (tgrG.text == ("g").toString())
&& (tgrE.text == ("e").toString()))
{
tiger = 1;
trace("tiger");
}
}
ALLwords = hippopotamus + horse + eel + frog + tiger + octopus + penguin + bird + orca +
turtle + duck + snail + ant + kangaroo + iguana + owl;
Total = 16 - ALLwords;
if (Total == 0)
{
trace("Gameover");
}
incorrectTXT.text = ("you have" + Total+ " incorrect words remaining").toString();
}

Angular one of the conditions is to check the password field for certain characters only [! # $% & *]

checkParams(event) {
if (event.length > 8 && event.length <= 30) {
this.passLength = true;
} else {
this.passLength = false;
}
//quantity check
if (event.search(/[0-9]/) > -1) {
this.passNumbers = true;
} else {
this.passNumbers = false;
}
//number check
if (event.search(/[!#$%&*]+/) > -1) {
this.passSpecial = true;
} else {
this.passSpecial = false;
}
//character check
if (event.search(/[a-z].*[A-Z]|[A-Z].*[a-z]/) > -1) {
this.passUpper = true;
} else {
this.passUpper = false;
}
//check upper case
if (event.search(/^\S*$/)) {
this.passSpace = false;
} else {
this.passSpace = true;
}
//check whitespace
if (event.search(/[!#$%&*]+/) > -1 && event.search(/[~`#^()_={}[\]:;,.<>+\/?-]+/) <= -1) {
this.passSpecial = true;
} else {
this.passSpecial = false;
}
You may try using the following single pattern:
^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!#$%&*]).{9,30}$
Your Angular JS code:
if (event.search(/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!#$%&*]).{9,30}$/) > -1) {
this.passValid = true;
}
else {
this.passValid = false;
}

How to ignore filtering for fields that are empty/undefined using filterPredicate

I am trying to understand the filterPredicate of MatTableDataSource, and when I thought I was close, I am missing some logic.
I want to filter through a datasource and if the array's value is blank or "", then it shouldn't filter for every value that is defined as "". In other words, filter with what it does know and not what it doesn't know.
I tried to assign the values to null if the length of the array is equal to 0. But even that did not work.
Typescript
this.registeredUserService.GetAllAdverts().subscribe(val => {
this.dataSource = new MatTableDataSource<Card>(val);
this.dataSource.paginator = this.paginator;
this.dataSource.filterPredicate = (myObject: IFilter, filterString: any) => {
let filterObj: IFilter = JSON.parse(filterString);
if (!filterObj.provinceName.includes(myObject.provinceName) ||
!filterObj.vehicleMake.includes(myObject.vehicleMake) ||
!filterObj.vehicleModel.includes(myObject.vehicleModel) ||
!filterObj.vehicleYear.includes(myObject.vehicleYear) ||
!filterObj.vehicleColor.includes(myObject.vehicleColor))
{
return false;
}
else {
return true;
}
}
filter()//whenever triggered, it should do the filtering
{
this.myFilter.provinceName = this.search.value.provinceSelector;
this.myFilter.vehicleMake = this.search.value.makeSelector;
this.myFilter.vehicleModel = this.search.value.modelSelector;
this.myFilter.vehicleColor = this.search.value.colorSelector;
this.myFilter.vehicleYear = this.search.value.yearSelector;
if (this.myFilter.provinceName.length == 0 &&
this.myFilter.vehicleMake.length == 0 &&
this.myFilter.vehicleModel.length == 0 &&
this.myFilter.vehicleColor.length == 0 &&
this.myFilter.vehicleYear.length == 0) {
this.dataSource.filter = '';
}
else {
this.dataSource.filter = JSON.stringify(this.myFilter);
}
}
myFilter: IFilter = {
provinceName: [],
vehicleMake: [],
vehicleModel: [],
vehicleColor: [],
vehicleYear: []
}
interface IFilter{
provinceName:any[],
vehicleMake:any[],
vehicleModel:any[],
vehicleColor:any[],
vehicleYear:any[]
}
What it should do: Filter based on my query
What it does: Only does filtering as soon as all the values are filled.
You just have to check the filter attribute before if it exists and length is greater 0 and when then search for it in your object.
this.registeredUserService.GetAllAdverts().subscribe(val => {
this.dataSource = new MatTableDataSource<Card>(val);
this.dataSource.paginator = this.paginator;
this.dataSource.filterPredicate = (myObject: IFilter, filterString: any) => {
let filterObj: IFilter = JSON.parse(filterString);
if (
(filterObj.provinceName && filterObj.provinceName.length > 0 && !filterObj.provinceName.includes(myObject.provinceName)) ||
(filterObj.vehicleMake && filterObj.vehicleMake.length > 0 && !filterObj.vehicleMake.includes(myObject.vehicleMake)) ||
(filterObj.vehicleModel && filterObj.vehicleModel.length > 0 && !filterObj.vehicleModel.includes(myObject.vehicleModel)) ||
(filterObj.vehicleYear && filterObj.vehicleYear.length > 0 && !filterObj.vehicleYear.includes(myObject.vehicleYear)) ||
(filterObj.vehicleColor && filterObj.vehicleColor.length > 0 && !filterObj.vehicleColor.includes(myObject.vehicleColor))
) {
return false;
} else {
return true;
}
}
});

Parse JSON string containing a colon

The class I am using to parse JSON does not allow you to have colons in any string. For example, this would cause an error in parsing:
{
"Test:": "Just a test"
}
Any ideas on how to make this class ignore colons that are in a string? Here is the class I am using:
class JSON {
static var inst;
var text;
function JSON() {
}
static function getInstance() {
if (inst == null) {
inst = new cinqetdemi.JSON();
}
return (inst);
}
static function stringify(arg) {
var _local3;
var _local2;
var _local6;
var _local1 = "";
var _local4;
switch (typeof (arg)) {
case "object" :
if (arg) {
if (arg instanceof Array) {
_local2 = 0;
while (_local2 < arg.length) {
_local4 = stringify(arg[_local2]);
if (_local1) {
_local1 = _local1 + ",";
}
_local1 = _local1 + _local4;
_local2++;
}
return (("[" + _local1) + "]");
} else if (typeof (arg.toString) != "undefined") {
for (_local2 in arg) {
_local4 = arg[_local2];
if ((typeof (_local4) != "undefined") && (typeof (_local4) != "function")) {
_local4 = stringify(_local4);
if (_local1) {
_local1 = _local1 + ",";
}
_local1 = _local1 + ((stringify(_local2) + ":") + _local4);
}
}
return (("{" + _local1) + "}");
}
}
return ("null");
case "number" :
return ((isFinite(arg) ? (String(arg)) : "null"));
case "string" :
_local6 = arg.length;
_local1 = "\"";
_local2 = 0;
while (_local2 < _local6) {
_local3 = arg.charAt(_local2);
if (_local3 >= " ") {
if ((_local3 == "\\") || (_local3 == "\"")) {
_local1 = _local1 + "\\";
}
_local1 = _local1 + _local3;
} else {
switch (_local3) {
case "\b" :
_local1 = _local1 + "\\b";
break;
case "\f" :
_local1 = _local1 + "\\f";
break;
case newline :
_local1 = _local1 + "\\n";
break;
case "\r" :
_local1 = _local1 + "\\r";
break;
case "\t" :
_local1 = _local1 + "\\t";
break;
default :
_local3 = _local3.charCodeAt();
_local1 = _local1 + (("\\u00" + Math.floor(_local3 / 16).toString(16)) + (_local3 % 16).toString(16));
}
}
_local2 = _local2 + 1;
}
return (_local1 + "\"");
case "boolean" :
return (String(arg));
}
return ("null");
}
static function parse(text) {
if (!text.length) {
throw new Error("JSONError: Text missing");
}
var _local1 = getInstance();
_local1.at = 0;
_local1.ch = " ";
_local1.text = text;
return (_local1.value());
}
function error(m) {
var _local2 = ((("JSONError: " + m) + " at ") + (at - 1)) + newline;
_local2 = _local2 + (text.substr(at - 10, 20) + newline);
_local2 = _local2 + " ^";
throw new Error(_local2);
}
function next() {
ch = text.charAt(at);
at = at + 1;
return (ch);
}
function white() {
while (ch) {
if (ch <= " ") {
next();
} else if (ch == "/") {
switch (next()) {
case "/" :
while ((next() && (ch != newline)) && (ch != "\r")) {
}
break;
case "*" :
next();
while (true) {
if (ch) {
if (ch == "*") {
if (next() == "/") {
next();
break;
}
} else {
next();
}
} else {
error("Unterminated comment");
}
}
break;
default :
error("Syntax error");
}
} else {
break;
}
}
}
function str() {
var _local5;
var _local2 = "";
var _local4;
var _local3;
var _local6 = false;
if ((ch == "\"") || (ch == "'")) {
var _local7 = ch;
while (next()) {
if (ch == _local7) {
next();
return (_local2);
} else if (ch == "\\") {
switch (next()) {
case "b" :
_local2 = _local2 + "\b";
break;
case "f" :
_local2 = _local2 + "\f";
break;
case "n" :
_local2 = _local2 + newline;
break;
case "r" :
_local2 = _local2 + "\r";
break;
case "t" :
_local2 = _local2 + "\t";
break;
case "u" :
_local3 = 0;
_local5 = 0;
while (_local5 < 4) {
_local4 = parseInt(next(), 16);
if (!isFinite(_local4)) {
_local6 = true;
break;
}
_local3 = (_local3 * 16) + _local4;
_local5 = _local5 + 1;
}
if (_local6) {
_local6 = false;
break;
}
_local2 = _local2 + String.fromCharCode(_local3);
break;
default :
_local2 = _local2 + ch;
}
} else {
_local2 = _local2 + ch;
}
}
}
error("Bad string");
}
function key() {
var _local2 = ch;
var _local6 = false;
var _local3 = text.indexOf(":", at);
var _local4 = text.indexOf("\"", at);
var _local5 = text.indexOf("'", at);
if (((_local4 <= _local3) && (_local4 > -1)) || ((_local5 <= _local3) && (_local5 > -1))) {
_local2 = str();
white();
if (ch == ":") {
return (_local2);
} else {
error("Bad key");
}
}
while (next()) {
if (ch == ":") {
return (_local2);
}
if (ch <= " ") {
} else {
_local2 = _local2 + ch;
}
}
error("Bad key");
}
function arr() {
var _local2 = [];
if (ch == "[") {
next();
white();
if (ch == "]") {
next();
return (_local2);
}
while (ch) {
if (ch == "]") {
next();
return (_local2);
}
_local2.push(value());
white();
if (ch == "]") {
next();
return (_local2);
} else if (ch != ",") {
break;
}
next();
white();
}
}
error("Bad array");
}
function obj() {
var _local3;
var _local2 = {};
if (ch == "{") {
next();
white();
if (ch == "}") {
next();
return (_local2);
}
while (ch) {
if (ch == "}") {
next();
return (_local2);
}
_local3 = this.key();
if (ch != ":") {
break;
}
next();
_local2[_local3] = value();
white();
if (ch == "}") {
next();
return (_local2);
} else if (ch != ",") {
break;
}
next();
white();
}
}
error("Bad object");
}
function num() {
var _local2 = "";
var _local3;
if (ch == "-") {
_local2 = "-";
next();
}
while (((((ch >= "0") && (ch <= "9")) || (ch == "x")) || ((ch >= "a") && (ch <= "f"))) || ((ch >= "A") && (ch <= "F"))) {
_local2 = _local2 + ch;
next();
}
if (ch == ".") {
_local2 = _local2 + ".";
next();
while ((ch >= "0") && (ch <= "9")) {
_local2 = _local2 + ch;
next();
}
}
if ((ch == "e") || (ch == "E")) {
_local2 = _local2 + ch;
next();
if ((ch == "-") || (ch == "+")) {
_local2 = _local2 + ch;
next();
}
while ((ch >= "0") && (ch <= "9")) {
_local2 = _local2 + ch;
next();
}
}
_local3 = Number(_local2);
if (!isFinite(_local3)) {
error("Bad number");
}
return (_local3);
}
function word() {
switch (ch) {
case "t" :
if (((next() == "r") && (next() == "u")) && (next() == "e")) {
next();
return (true);
}
break;
case "f" :
if ((((next() == "a") && (next() == "l")) && (next() == "s")) && (next() == "e")) {
next();
return (false);
}
break;
case "n" :
if (((next() == "u") && (next() == "l")) && (next() == "l")) {
next();
return (null);
}
break;
}
error("Syntax error");
}
function value() {
white();
switch (ch) {
case "{" :
return (obj());
case "[" :
return (arr());
case "\"" :
case "'" :
return (str());
case "-" :
return (num());
}
return ((((ch >= "0") && (ch <= "9")) ? (num()) : (word())));
}
var at = 0;
var ch = " ";
}
Take a look on my answer of this question and use the JSON class that I mentioned there, it's working fine with your test content :
import JSON;
var json = new JSON();
var data:String = '{"Test:": "Just a test"}';
trace(json.parse(data)['Test:']); // gives : Just a test
Hope that can help.

Breakpoint hit twice in MVC Action

I am working on mvc project in razor.In this i am working on import and export module.
Now in my action i am returning JSON object.Now in both Http Get Request or Http Post request i am getting the same problem.Because the breakpoint is hitting twice,my record get added in database twice.
Below is my script
<script type="text/javascript">
$(document).ready(function (e) {
$(".update").live('click', function () {
var id = $(this).attr('id');
var str = "";
$(this).parent('td').parent('tr').find('td.data').each(function () {
str = str + ";" + $(this).text().trim();
});
if (str.length > 0) {
str = str.substring(1, str.length);
}
var header = $("#hdn_header").val();
$.post('/ImportCSV/Update', {
ImportData: str,
ImportHeader: header
}, function (result) {
var CompName = result.CompanyName;
var CompPhone = result.CompanyPhone;
var CompEmail = result.CompanyEmail;
var CompWebsite = result.CompanyWebsite;
var CompAddress = result.CompanyAddress;
var CompZip = result.CompanyZip;
var CompCity = result.CompanyCity;
var CompCountry = result.CompanyCountry;
var CompNote = result.CompanyNote;
var ConFirstName = result.ConFirstName;
var ConLastName = result.ConLastName;
var ConTitle = result.ConTitle;
var ConPhone = result.ConPhone;
var ConEmail = result.ConEmail;
var ConNote = result.ConNote;
var ConMobile = result.ConMobile;
$(".update").each(function () {
if ($(this).attr('id') == id) {
var i = parseInt('1');
$(this).parent('td').parent('tr').find('td.data').each(function () {
if ($(this).text().trim() != CompName && $(this).index() == 2) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompPhone && $(this).index() == 3) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompEmail && $(this).index() == 4) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompWebsite && $(this).index() == 5) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompAddress && $(this).index() == 6) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompZip && $(this).index() == 7) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompCity && $(this).index() == 8) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompCountry && $(this).index() == 9) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != CompNote && $(this).index() == 10) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConFirstName && $(this).index() == 12) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConLastName && $(this).index() == 13) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConTitle && $(this).index() == 14) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConPhone && $(this).index() == 15) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConMobile && $(this).index() == 16) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConEmail && $(this).index() == 17) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
if ($(this).text().trim() != ConNote && $(this).index() == 18) {
{
$(this).css('backgroundColor', '#FFFFC0');
return;
}
}
});
}
});
});
return false;
});
});
</script>
The above is my whole script and again it is giving same error.pls check and let me know if i am doing wrong.
Thank you..
It is not very clear where exactly are you invoking this AJAX request but I suspect that it is inside the .click event of some anchor or a .submit button and you didn't cancel the default action of this button by returning false from the handler. So when the link is clicked you get a first request with the AJAX POST and immediately after the default action of the link is executed.
So for example if you were subscribing to the .submit event of some form try canceling the default action by returning false:
$(function() {
$('#someForm').submit(function() {
$.post('/ImportCSV/Update', { ImportData: str, ImportHeader: header }, function (result) {
var CompName = result.CompanyName;
});
return false; // <-- that's the important part
});
});