Formatted Version of ECMAScript (JavaScript) Code
This page contains some Badly Formatted ECMAScript (JavaScript) code, and its ECMAScript Formatted version. The output was produced by our ECMAScript Formatter.
Badly Formatted ECMAScript (JavaScript) Code
This is a calculator, believe it or not. The nesting structure of this code is truly ugly; most programmers will have to reformat it manually to figure out what is going on. There goes hour of wasted time, if you don't make any mistakes.
LastOper = ''; TempVal = 0; NewNum = 'Y'; Memval = 0; Dotused= "N"; Curroption=4; Maxoption = 19; FirstinXonY = ''; Statelements = [0]; n = 0; Ave = 0; Max = 1.1111111234e1; Min = 1.1111111234; Dummymaxmin = 1.1111111234; Sum = 0; additionalfunctions = ["x^2", "2^x", "x^y", "x^3", " n! ", "Log", " Pi ", " h ", "Tan", "Cot", "Sin", "Cos", "Add", "Sum", "Ave", "Max", "Min", " n ", "Clr St", "Sta"]; function backspace() { strlen = document.calc.Results. value.length; if (strlen > 1 && document.calc.Results.value != "0."){document.calc.Results.value = document.calc.Results.value.substring(0,strlen-1);} document.calc.EQ.focus(); } function Onediv() { if (document.calc.Results.value == '0.') { document.calc.Results.value = "Can't Devide By 0"; } else { document.calc.Results.value = 1 / document.calc.Results.value; NewNum = "Y"; } document.calc.EQ.focus(); } function MoreOptions() { if (Curroption > Maxoption) {Curroption = 0;}document.calc.opt1.value = additionalfunctions[Curroption++]; if (Curroption > Maxoption) {Curroption = 0;}document.calc.opt2.value = additionalfunctions[Curroption++]; if (Curroption > Maxoption) {Curroption = 0;}document.calc.opt3.value = additionalfunctions[Curroption++]; if (Curroption > Maxoption) {Curroption = 0;}document.calc.opt4.value = additionalfunctions[Curroption++]; document.calc.EQ.focus();} function Advfunction(Pos) { Pos += Curroption; Pos -= 4; if (Pos < 0) {Pos = Maxoption + Pos;} if (Pos == 0) {document.calc.Results.value *= document.calc.Results.value;} if (Pos == 1) { Temp = 1; for (var I = 0 ;I < document.calc.Results.value; I++) { Temp *= 2; } document.calc.Results.value = Temp; } if (Pos == 2) {if (FirstinXonY == '') { FirstinXonY = document.calc.Results.value; }else { Temp = FirstinXonY; for (var I = 1 ;I < document.calc.Results.value; I++) { Temp *= FirstinXonY; } document.calc.Results.value = Temp; FirstinXonY = ''; } } if (Pos == 4){Temp = 0; for (var I = 1; I <= document.calc.Results.value;I++) {Temp += I} document.calc.Results.value = Temp;} if (Pos == 6){document.calc.Results.value = 3.14159265359;} if (Pos == 12) { Statelements[++n] = document.calc.Results.value; Expres = "Sum = + +" + Sum + " + " + "+" + document.calc.Results.value; eval(Expres); Ave = Sum/n; Max = 1 * Max; Min = 1 * Min; if (Max < document.calc.Results.value || Max == Dummymaxmin) {Max = document.calc.Results.value;} if (Min > document.calc.Results.value || Min == Dummymaxmin) {Min = document.calc.Results.value;} } if (Pos == 13){document.calc.Results.value = Sum;} if (Pos == 14){document.calc.Results.value = Ave;} if (Pos == 15){if (n > 0) {document.calc.Results.value = Max;}} if (Pos == 16){if (n > 0) {document.calc.Results.value = Min;}} if (Pos == 17){document.calc.Results.value = n;} if (Pos == 18) { Statelements = [0]; n = 0; Ave = 0; Max = 1.1111111234; Min = 1.1111111234; Dummymaxmin = 1.1111111234; Sum = 0; } if (Pos == 19) { if (n > 0) { myWindow = window.open("", "newwin", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=200,height=180,left=400,top=300"); myWindow.document.open(); myWindow.document.write("<HTML><HEAD>"); myWindow.document.write("<TITLE>Statistics</TITLE>"); myWindow.document.write("</HEAD><FORM><BODY BGCOLOR=#FFFFFF TEXT=black>\n<CENTER>"); myWindow.document.write("<TABLE ALIGN=CENTER>"); myWindow.document.write("<TR><TD><B>n</B></TD><TD>: " , n , "</TD></TR>"); myWindow.document.write("<TR><TD><B>Sum</B></TD><TD>: ", Sum, "</TD></TR>"); myWindow.document.write("<TR><TD><B>Ave</B></TD><TD>: ", Ave, "</TD></TR>"); myWindow.document.write("<TR><TD><B>Max</B></TD><TD>: ", Max, "</TD></TR>"); myWindow.document.write("<TR><TD><B>Min</B></TD><TD>: ", Min, "</TD></TR>"); myWindow.document.write("</TABLE>"); myWindow.document.write(" <BR><INPUT TYPE='SUBMIT' VALUE='OK' onClick='window.close()'</FORM>\n</CENTER>"); myWindow.document.write("</BODY></HTML>"); myWindow.document.close(); } else { myWindow = window.open("", "newwin", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=200,height=180,left=400,top=300"); myWindow.document.open(); myWindow.document.write("<HTML><HEAD>"); myWindow.document.write("<TITLE> No Statistics</TITLE>"); myWindow.document.write("</HEAD><FORM><BODY BGCOLOR=#FFFFFF TEXT=BLACK>\n<CENTER>"); myWindow.document.write("<U><B>No Statistics</B></U><BR><BR>You did not add any item<BR>to the statistics<BR>To add items, use The<BR>'Add' Button"); myWindow.document.write("<BR><BR><INPUT TYPE='SUBMIT' VALUE='OK' onClick='window.close()'</FORM></CENTER>"); myWindow.document.write("</BODY></HTML>"); myWindow.document.close(); } } NewNum = 'Y'; } function SQRT() { document.calc.Results.value = sqrt( document.calc.Results.value); NewNum = "Y"; document.calc.EQ.focus();} function Percent() { document.calc.Results.value *= TempVal / 100; Dotused = "N"; NewNum = "Y"; document.calc.EQ.focus(); } function Oper(OP) { if (LastOper == "+") { num1 = document.calc.Results.value; Command = eval ("'+' + num1 + '+' + TempVal"); document.calc.Results.value= eval (Command); } if (LastOper == "-") { num1 = document.calc.Results.value; Command = "(document.calc.Results.value = TempVal - num1)"; eval (Command); } if (LastOper == "*") { num1 = document.calc.Results.value; Command = "(document.calc.Results.value = TempVal * num1)"; eval (Command); } if (LastOper == "/") { if (document.calc.Results.value == '0.') { document.calc.Results.value = "Can't Devide By 0"; } else { num1 = document.calc.Results.value; Command = "(document.calc.Results.value = TempVal / num1)"; eval (Command); } } if (OP != "="){document.calc.oper.value = " " + OP;} else { document.calc.oper.value =""; } NewNum = "Y"; LastOper = OP; TempVal = document.calc.Results.value ; document.calc.EQ.focus(); FirstinXonY = '';} function Analyzethis(Key) { if(Key>="0"&&Key<="9"){Digit(Key);}if(Key=="*"||Key=="-"||Key=="\\"||Key =="+"||Key=="="){Oper(Key);}document.calc.EQ.focus();} function Memory(Mode) { if (Mode == "Clear") { document.calc.Mem.value=""; Memval = 0; } if (Mode == "Set") { document.calc.Mem.value = "M";Memval = document.calc.Results.value; } if (Mode == "Add") { document.calc.Mem.value = "M"; Command = eval("'+' + document.calc.Results.value + '+' + Memval"); Memval = eval (Command); } if (Mode == "Read") { document.calc.Results.value = Memval; NewNum = "Y"; } document.calc.EQ.focus();} function Dot(){ if (NewNum == "Y") { document.calc.Results.value = "0."; Dotused = "Y"; NewNum = "N"; } if (Dotused == "N") { document.calc.Results.value += "."; Dotused = "Y"; } document.calc.EQ.focus();} function Changesign(){ document.calc.Results.value *= -1; document.calc.EQ.focus();} function Digit(DG){ if (NewNum == "Y") { document.calc.Results.value=DG; Dotused = "N"; } else { document.calc.Results.value= document.calc.Results.value + DG; } if (document.calc.Results.value == '0') { document.calc.Results.value = '0.' } else { NewNum = "N"; } document.calc.EQ.focus();} function Clear (Type ){ document.calc .Results.value = '0.'; NewNum = 'Y' ; if (Type == "ALL") { document.calc.oper.value =""; LastOper = ''; TempVal = 0; } document.calc.EQ.focus(); }
ECMAScript (JavaScript) Formatted Version
This is the result of using SD's ECMAScriptFormatter tool on the sample badly formatted code, using just the default settings. You can see that the formatter has chosen very different line breaks, based on the language structure. The block structure is now clearly visible. A programmer might actually be able to work on this version. While this particular example, like most ECMAScript code, doesn't have any comments, the ECMAFormatter will preserve and properly indent any comments it finds in the source.
LastOper = ""; TempVal = 0; NewNum = "Y"; Memval = 0; Dotused = "N"; Curroption = 4; Maxoption = 19; FirstinXonY = ""; Statelements = [0]; n = 0; Ave = 0; Max = 1.1111111234e1; Min = 1.1111111234; Dummymaxmin = 1.1111111234; Sum = 0; additionalfunctions = ["x^2","2^x","x^y","x^3"," n! ","Log"," Pi "," h ","Tan","Cot","Sin","Cos","Add","Sum","Ave","Max","Min"," n ","Clr St","Sta"]; function backspace() { strlen = document.calc.Results.value.length; if (strlen > 1 && document.calc.Results.value != "0.") { document.calc.Results.value = document.calc.Results.value.substring(0,strlen-1); } document.calc.EQ.focus(); } function Onediv() { if (document.calc.Results.value == "0.") { document.calc.Results.value = "Can\'t Devide By 0"; } else { document.calc.Results.value = 1/document.calc.Results.value; NewNum = "Y"; } document.calc.EQ.focus(); } function MoreOptions() { if (Curroption > Maxoption) { Curroption = 0; } document.calc.opt1.value = additionalfunctions[Curroption++ ]; if (Curroption > Maxoption) { Curroption = 0; } document.calc.opt2.value = additionalfunctions[Curroption++ ]; if (Curroption > Maxoption) { Curroption = 0; } document.calc.opt3.value = additionalfunctions[Curroption++ ]; if (Curroption > Maxoption) { Curroption = 0; } document.calc.opt4.value = additionalfunctions[Curroption++ ]; document.calc.EQ.focus(); } function Advfunction(Pos) { Pos += Curroption; Pos -= 4; if (Pos < 0) { Pos = Maxoption+Pos; } if (Pos == 0) { document.calc.Results.value *= document.calc.Results.value; } if (Pos == 1) { Temp = 1; for ( var I = 0;I < document.calc.Results.value;I++ ) { Temp *= 2; } document.calc.Results.value = Temp; } if (Pos == 2) { if (FirstinXonY == "") { FirstinXonY = document.calc.Results.value; } else { Temp = FirstinXonY; for ( var I = 1;I < document.calc.Results.value;I++ ) { Temp *= FirstinXonY; } document.calc.Results.value = Temp; FirstinXonY = ""; } } if (Pos == 4) { Temp = 0; for ( var I = 1;I <= document.calc.Results.value;I++ ) { Temp += I } document.calc.Results.value = Temp; } if (Pos == 6) { document.calc.Results.value = 3.14159265359; } if (Pos == 12) { Statelements[ ++n] = document.calc.Results.value; Expres = "Sum = + +"+Sum+" + "+"+"+document.calc.Results.value; eval(Expres); Ave = Sum/n; Max = 1*Max; Min = 1*Min; if (Max < document.calc.Results.value || Max == Dummymaxmin) { Max = document.calc.Results.value; } if (Min > document.calc.Results.value || Min == Dummymaxmin) { Min = document.calc.Results.value; } } if (Pos == 13) { document.calc.Results.value = Sum; } if (Pos == 14) { document.calc.Results.value = Ave; } if (Pos == 15) { if (n > 0) { document.calc.Results.value = Max; } } if (Pos == 16) { if (n > 0) { document.calc.Results.value = Min; } } if (Pos == 17) { document.calc.Results.value = n; } if (Pos == 18) { Statelements = [0]; n = 0; Ave = 0; Max = 1.1111111234; Min = 1.1111111234; Dummymaxmin = 1.1111111234; Sum = 0; } if (Pos == 19) { if (n > 0) { myWindow = window.open("","newwin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=200,height=180,left=400,top=300"); myWindow.document.open(); myWindow.document.write("<HTML><HEAD>"); myWindow.document.write("<TITLE>Statistics</TITLE>"); myWindow.document.write("</HEAD><FORM><BODY BGCOLOR=#FFFFFF TEXT=black>\n<CENTER>"); myWindow.document.write("<TABLE ALIGN=CENTER>"); myWindow.document.write("<TR><TD><B>n</B></TD><TD>: ",n,"</TD></TR>"); myWindow.document.write("<TR><TD><B>Sum</B></TD><TD>: ",Sum,"</TD></TR>"); myWindow.document.write("<TR><TD><B>Ave</B></TD><TD>: ",Ave,"</TD></TR>"); myWindow.document.write("<TR><TD><B>Max</B></TD><TD>: ",Max,"</TD></TR>"); myWindow.document.write("<TR><TD><B>Min</B></TD><TD>: ",Min,"</TD></TR>"); myWindow.document.write("</TABLE>"); myWindow.document.write(" <BR><INPUT TYPE=\'SUBMIT\' VALUE=\'OK\' onClick=\'window.close()\'</FORM>\n</CENTER>"); myWindow.document.write("</BODY></HTML>"); myWindow.document.close(); } else { myWindow = window.open("","newwin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=200,height=180,left=400,top=300"); myWindow.document.open(); myWindow.document.write("<HTML><HEAD>"); myWindow.document.write("<TITLE> No Statistics</TITLE>"); myWindow.document.write("</HEAD><FORM><BODY BGCOLOR=#FFFFFF TEXT=BLACK>\n<CENTER>"); myWindow.document.write("<U><B>No Statistics</B></U><BR><BR>You did not add any item<BR>to the statistics<BR>To add items, use The<BR>\'Add\' Button"); myWindow.document.write("<BR><BR><INPUT TYPE=\'SUBMIT\' VALUE=\'OK\' onClick=\'window.close()\'</FORM></CENTER>"); myWindow.document.write("</BODY></HTML>"); myWindow.document.close(); } } NewNum = "Y"; } function SQRT() { document.calc.Results.value = sqrt(document.calc.Results.value); NewNum = "Y"; document.calc.EQ.focus(); } function Percent() { document.calc.Results.value *= TempVal/100; Dotused = "N"; NewNum = "Y"; document.calc.EQ.focus(); } function Oper(OP) { if (LastOper == "+") { num1 = document.calc.Results.value; Command = eval("\'+\' + num1 + \'+\' + TempVal"); document.calc.Results.value = eval(Command); } if (LastOper == "-") { num1 = document.calc.Results.value; Command = "(document.calc.Results.value = TempVal - num1)"; eval(Command); } if (LastOper == "*") { num1 = document.calc.Results.value; Command = "(document.calc.Results.value = TempVal * num1)"; eval(Command); } if (LastOper == "/") { if (document.calc.Results.value == "0.") { document.calc.Results.value = "Can\'t Devide By 0"; } else { num1 = document.calc.Results.value; Command = "(document.calc.Results.value = TempVal / num1)"; eval(Command); } } if (OP != "=") { document.calc.oper.value = " "+OP; } else { document.calc.oper.value = ""; } NewNum = "Y"; LastOper = OP; TempVal = document.calc.Results.value; document.calc.EQ.focus(); FirstinXonY = ""; } function Analyzethis(Key) { if (Key >= "0" && Key <= "9") { Digit(Key); } if (Key == "*" || Key == "-" || Key == "\\" || Key == "+" || Key == "=") { Oper(Key); } document.calc.EQ.focus(); } function Memory(Mode) { if (Mode == "Clear") { document.calc.Mem.value = ""; Memval = 0; } if (Mode == "Set") { document.calc.Mem.value = "M"; Memval = document.calc.Results.value; } if (Mode == "Add") { document.calc.Mem.value = "M"; Command = eval("\'+\' + document.calc.Results.value + \'+\' + Memval"); Memval = eval(Command); } if (Mode == "Read") { document.calc.Results.value = Memval; NewNum = "Y"; } document.calc.EQ.focus(); } function Dot() { if (NewNum == "Y") { document.calc.Results.value = "0."; Dotused = "Y"; NewNum = "N"; } if (Dotused == "N") { document.calc.Results.value += "."; Dotused = "Y"; } document.calc.EQ.focus(); } function Changesign() { document.calc.Results.value *= -1; document.calc.EQ.focus(); } function Digit(DG) { if (NewNum == "Y") { document.calc.Results.value = DG; Dotused = "N"; } else { document.calc.Results.value = document.calc.Results.value+DG; } if (document.calc.Results.value == "0") { document.calc.Results.value = "0." } else { NewNum = "N"; } document.calc.EQ.focus(); } function Clear(Type) { document.calc.Results.value = "0."; NewNum = "Y"; if (Type == "ALL") { document.calc.oper.value = ""; LastOper = ""; TempVal = 0; } document.calc.EQ.focus(); }