The implementation of basic JavaScript exercises C Language Classic program problems

Source: Internet
Author: User

Objective:

Recently in the language of learning JavaScript, see a lot of online is in the introduction of JavaScript How to solve the problem on the page code, so want to find a way to use JavaScript code to implement the C language Classic program problems. Of course, these C language program is relatively simple, mainly want to achieve through the JavaScript language, play the role of grammar practice, but also want to compare the C language and JavaScript implementation of the same point and different points, so as to consolidate memory, enhance learning effect!!!

One, C language Classic program question 1
1. Title Description:

Marx's manuscript has such an interesting math problem: There are 30 people, including men, women, children. They ate at a restaurant and spent 50 shillings a total. If every man eats 3 shillings, every woman spends 2 shillings, and every child spends 1 shillings, asking how many men, women, and children?

2. JavaScript code:
VarMans, woman, child; for(Mans=0;Mans< -;Mans++) {     for(woman =0; Woman <= -; Woman + +) { child= --Mans-woman; if( (Mans+ woman + Child = = -) && (Mans*3+ Woman *2+ Child = = -) {document.Write("Men:"+Mans+","+"Women:"+woman+","+"Children:"+child +"</br>"); }     }}        
3. Explanation of Problem Solving:

This topic is a very classic topic, with "Change Change" is the same type of topic, only need multiple loops plus a judge can list each result. From this problem, JavaScript code and C language code basically no difference, because for statement and if statement, JavaScript language and C language is the same. The main differences are now, the definition of variables and output statements, the overall sense of JavaScript language implementation is more convenient, mainly due to JavaScript weak type language, C language is strongly typed language.

Second, C language Classic program question 2 1. Title Description:

Calculate e=1+1/1! +1/2! +1/3! ... +1/n! The first 50 items

2. JavaScript code:
var N; var s = 1; var e = 1;  for (n = 1; n <=; n + +) {    = s * n;     = e + (1/ s);} document.write (e);

3. Explanation of Problem Solving:

This topic is a relatively simple topic, the implementation also uses only a For loop, and the C language code, the difference between the variable definition and output.

three, C language Classic program question 3 1. Title Description:

Enter a number (an unlimited number of digits) to output a number of digits.

2. JavaScript code:
functionDemo () {varn = document.getElementById ("number"). Value; if(!IsNaN (n)) {varLen =n.length; }            Else{alert ("Please enter a number!" "); return; } document.getElementById ("Number"). Value =Len; }    </script>3. Explanation of Problem Solving:

This topic is more interesting, if implemented in C language, it is necessary to constantly give this number to 10 evenly divisible, has reached the goal of obtaining a digital length. But the weakly typed nature of JavaScript makes it particularly easy to solve this problem with JavaScript, where the default number is the string type, as long as the length of the string is judged. Although a bit lazy, but also realized the function. In addition, the implementation of this topic also appeared and C language different things, is to solve the input problem, C language use is scanf, but JavaScript does not have such functions, so only the use of Web pages to achieve digital input.

Four, C language Classic program question 4 1. Title Description:

Print 99 multiplication table

2. JavaScript code:
function Demo () {    var  I, J, S;      for (i = 1; i <; i + +)    {        for (j = 1; J <= I; j + +)        {            = i * J;            document.write (J+ "*" +i+ "=" +s+ "");        }        document.write ("<br/>");}    }    

3. Explanation of Problem Solving:

This is a very classic topic, but the implementation is particularly simple, only need two loops nested on it. In addition to the definition and output of the variable, there is one more, that is, a newline. The C-Word line-up is OK, but the page does not recognize \ n, so you can only use the <br/> of HTML to implement line wrapping.

FiveC Language Classic program question 51. Title Description:

Young singers participate in the song Grand Prix, with 10 judges scoring, try programming to find the average score (remove a maximum score and a minimum score)

2. JavaScript code:

<body><input id= "Getscore" type= "text" ><button onclick= "demo ()" > Average </button><p id= "TXT" ></p><script>functionDemo () {varstr = document.getElementById ("Getscore"). Value; varScore =NewArray (); Score= Str.split (","); varMax = 0; varMin = 10000; varsum = 0; varAve = 0;  for(i=0;i<score.length;i++){            if(score[i]>max) {Max=Score[i]; }            if(score[i]<min) {min=Score[i]; } Sum= sum+Score[i]; } Ave= (sum-max-min)/8; document.getElementById ("TXT"). InnerHTML =Ave; }</script></body>

3. Explanation of Problem solving

This topic should be counted as the number of code, although the topic is very simple, but in the implementation of the input 10 results encountered difficulties, because the C language can not be a single input. So, I use input 1,2,3,4,5,6,7,8,9,10, a comma to enter 10 at a time. After the input, it is necessary to split the string, so we used the split function.

FiveSummary

Finally written in JavaScript language 5 C language Classic topics, is also a good start to learn JavaScript. Back in the process of solving each problem, I feel that JavaScript is really very C language very much like, so start relatively simple, but in the processing of input and output, a little different. If I were to comment, JavaScript and C that good? I would think that JavaScript is really easier to solve the problem faster, I prefer its weak type feature, and no longer have to worry about declaring the variable type error. Of course, C language as the Eternal Classic language, of course, is also very good.

JavaScript basic exercises to implement C language Classic program questions

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.