Javascript: Typical C language programming _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to solve typical Programming Problems in C language in javascript. If you are interested, please refer to the recent study of the Javascript language, many of the Code on the internet is about how Javascript solves the problem on the web page, so I want to use Javascript code to implement the typical C language program. Of course, these C language program questions are also relatively simple, mainly intended to be implemented through the Javascript language, play a role in syntax exercises, but also want to compare the similarities and differences between the C language and the Javascript language, so as to consolidate the memory, learn more !!!

I. Typical C language program Question 1

1. Question description:

There is an interesting mathematical question in Marx's manuscript: 30 people, including men, women, and children. They spent a total of 50 shbs for dinner in a restaurant. If a man takes three shbs for dinner, every woman needs two shbs, and every child needs one shbs, how many men, women, and children are asked?

2. Javascript code:

Var man, woman, child; for (man = 0; man <17; man ++) {for (woman = 0; woman <= 25; woman ++) {child = 30-man-woman; if (man + woman + child = 30) & (man * 3 + woman * 2 + child = 50 )) {document. write ("man:" + man + "," + "woman:" + woman + "," + "child:" + child +"
");}}}

3. Solution description:

This is a classic question. It is the same type of question as "change". You only need to add one judgment for multiple cycles to list each result. From this question, there is basically no difference between the Javascript code and the C language code, because the for statement and the if statement, the Javascript language and the C language are the same. The main difference lies in the definition of variables and the output statements. In general, Javascript is easier to implement, mainly because of the weak Javascript language, and C language is a strong language.

Ii. Typical C language program Question 2

1. Question 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 <= 50; n ++){  s = s * n;  e = e + (1 / s);}document.write(e); 

3. Solution description:

This is a simple question, and only a for loop is used for implementation. Compared with the C language code, the difference lies in variable definition and output.

Iii. Typical C language program question 3

1. Question description:

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

2. Javascript code:

  
   Typical C language question 3Script function demo () {var n = document. getElementById ("number"). value; if (! IsNaN (n) {var len = n. length;} else {alert ("enter a number! "); Return;} document. getElementById (" number "). value = len;} script    Click me.

3. Solution description:

This question is interesting. If it is implemented in C language, we need to constantly divide the number by 10 to get the length of the number. However, the weak type of Javascript makes it very easy to solve this problem using Javascript. The default number is the string type, and you only need to judge the length of the string. Although it is a bit lazy, it also implements the function. In addition, the implementation of this question is also different from the C language, that is, to solve the input problem. The C language uses scanf, but Javascript does not have such a function, so you can only use the web page to input numbers.

4. Typical C language program question 4

1. Question description:

Print the 9-9 multiplication table

2. Javascript code:

function demo(){  var i, j, s;  for(i = 1; i < 10; i ++)  {    for(j = 1; j <= i; j ++)    {      s = i * j;      document.write(j+"*"+i+"="+s+" ");    }    document.write("
"); }}

3. Solution description:

This is also a classic question, but it is very simple to implement. It only requires two loops and nesting. In addition to variable definition and output, there is also a line feed. You only need to use the line feed of the C language, but the web page is not recognized, so you can only use the html
To implement line feed.

V. Typical C language program Question 5

1. Question description:

Young Singers took part in the Song Grand Prix and scored by 10 judges. They tried programming to calculate the average score of contestants (excluding one highest score and one lowest score)

2. Javascript code:

Average score

Script function demo () {var str = document. getElementById ("getScore "). value; var score = new Array (); score = str. split (","); var max = 0; var min = 10000; var sum = 0; var ave = 0; for (I = 0; I Max) {max = score [I];} if (score [I]

3. Solution description

This question is probably the most code-intensive. Although the question is simple, it is difficult to input ten scores, because it cannot be entered one by one like the C language. Therefore, I enter 10 entries at a time using commas (,), 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. After the input, the string is split, so the split function is used.

V. Summary

Finally, I used the Javascript language to write five typical C language questions, which is also a good start to learn Javascript. Looking back at the process of solving each question, I feel that Javascript is really very similar to the C language, so it is relatively simple to start with, but it is a little different when processing input and output. What should I do if I want to comment on Javascript and C? I think Javascript is easier and faster to solve the problem. I like its weak type features and don't worry about declaring variable type errors any more. C language, of course, is also a good classical language.

The above is all the content of this article, hoping to help you learn.

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.