Big talk design pattern described in Javascript language (1)

Source: Internet
Author: User

I have read the online trial part of this book. I used JavaScript to rewrite the C # source code in this book.

Chapter 1 source code rewriting in section 1.1:

Code
Program = {
Main: function (){
If (! Document. getelementbyid) return;
VaR A = Document. getelementbyid ('numa'). value;
VaR B = Document. getelementbyid ('operator'). value;
VaR c = Document. getelementbyid ('numb'). value;
VaR d = '';
If (B = '+ '){
D = Number (A) + number (C );
}
If (B = '-'){
D = Number (a)-number (C );
}
If (B = '*'){
D = Number (a) * Number (C );
}
If (B = '/'){
D = Number (a)/number (C );
}
Document. getelementbyid ('compute'). onclick = function (){
Return document. getelementbyid ('result'). innerhtml = D;
}
}
}

HTML code:

Code
<P>
<Label for = "NUMA"> enter the number A: </label>
<Input type = "text" id = "NUMA" value = ""/>
</P>
<P>
<Label for = "operator"> select the operator number (+,-, *,/): </label>
<Input type = "text" id = "operator" value = ""/>
</P>
<P>
<Label for = "Numb"> enter the number B: </label>
<Input type = "text" id = "Numb" value = ""/>
</P>
<P>
<Button id = "compute"> computing </button>
</P>
<P>
<Span id = "result"> </span>
</P>

 

Chapter 1 source code rewriting in section 1.3:

Code
Program = {
Main: function (){
If (! Document. getelementbyid) return;
Try {
VaR snuma = Document. getelementbyid ('numa'). value;
VaR soperate = Document. getelementbyid ('operate'). value;
VaR snumb = Document. getelementbyid ('numb'). value;
VaR sresult = '';
Switch (soperate ){
Case '+ ':
Sresult = (number (snuma) + number (snumb). tostring ();
Break;
Case '-':
Sresult = (number (snuma)-number (snumb). tostring ();
Break;
Case '*':
Sresult = (number (snuma) * Number (snumb). tostring ();
Break;
Case '/':
If (snumb! = '0 ')
Sresult = (number (snuma)/number (snumb). tostring ();
Else
Sresult = 'divisor cannot be 0 ';
Break;
}
}
Catch (Ex ){
Console. Log ('your input is incorrect: '+ ex );
}
Document. getelementbyid ('compute'). onclick = function (){
Return document. getelementbyid ('result'). innerhtml = sresult;
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.