質問: Ugly Code I want to convert a binary value to a decimal value, but it seems to lengthy for me as I written the below ugly code, which is frusrating and constraining, any one with a short and nice code. ``` My First Web Page Display Date ```
回答: wow, this code looks like someone obfuscated it, to make it unreadable. I'd advice you to use variable names that clearly say what they do, so ``` var a=document.getElementById("text").value var b=a.charAt(0); var q=a.charAt(1); var bq=a.charAt(2); var xy= a.charAt(3); ``` would be ``` var text=document.getElementById("text").value var firstletter =text.charAt(0); var secondletter =text.charAt(1); var thirdletter =text.charAt(2); var fourthletter =text.charAt(3); ``` (placing the equals signs underneath each other isn't necessary; im being perfectionistic :) ) Also, while going through your code to correct those variables, it might be a good idea to check what you need or not. for example, the commented alerts can be removed. shorter code looks less depressing when having to hunt for bugs etc. Finally, it might be usefull to put comments at certain points, telling you what you expect to have on the screen, or atleast in the memory. This wil also make it easier for others to understand if you ever have to code in a group.