Rus Articles Journal

Whether it is possible to begin to program in 5 minutes? We study on rebuses and dates of

we Will continue studying of bases of programming on the example of the Javascript language. As it was told in the first part, the main plus of such approach - in lack of need to install any programs on the computer, there is enough Notebook and Internet Explorer browser.

Any of the programs provided in the text can be gathered in the Notebook editor, to keep in the form of the file with the HTML expansion, further it is necessary to start the browser double click according to the saved file. Everything, it is enough!

So, we will continue.

Mathematical rebuses

was considered by

In the first part “a magic square“, more precisely a way of its receiving by means of the computer. Let`s review an example of other numerical task shown on the first screenshot. It is necessary to solve a rebus “the FLY / HA = FISH SOUP“ in which each letter corresponds to one figure. It is required to find such figures at which substitution instead of letters expression becomes true.

The way of the decision by means of the computer is very simple: by means of the cycles for we will touch all 9999 options for each of 4 - x the figures entering the word “FLY“. We use 4 variables M, U, H and A for storage of figures from 0 to 9, then mathematically the first can be written down as 1000*M + 100*U + 10*X + And. Numbers HA and FISH SOUP are coded similarly then to check result enough one comparison. The code of the program is shown in drawing, we will not provide the answer of a rebus, readers will be able to find it independently, having executed the program.

Use of lines

the Important aspect of any programming language is work with lines. In any language there are such functions as obtaining length of a line, search of a subline, transformation of a line to number and so forth. Let`s review a simple example of a conclusion of a line in various options.

We will use the length and charAt functions returning length of a line and a symbol with the necessary index. For a start we will remove a line symbol-by-symbol for what we use the cycle for which will pass the range of values from 0 to length (for those who are absolutely unfamiliar with programming, we will remind that as well as in language “C“, numbering of many objects goes to Javascript from scratch, i.e. str. charAt(0) will return the first symbol in line). The code for a conclusion is shown in the second drawing.

It would seem, there is no advantage in it, the line can be removed also usual, easier way. However, having complicated a code, we can draw a conclusion more various, for example, to paint each letter in separate color. For this purpose we will use HTML - “font color=“ #aabbcc code““ where aabbcc are the components of color calculated in a random way (as Javascript is carried out in the browser, we can use HTML tags for registration of the text). For receiving a random number we will use the Math function. random returning value in the range from 0 to 1. After start of the program we will see a line in which each letter is painted in the color, as shown in the following screenshot.

And at last, the third way of a conclusion which we use - a conclusion of a line upside-down. The code differs from the first option only in one line, we suggest readers to understand a difference independently.

Use of dates

the Possibility of work with dates also is in all modern languages of programming. In Javascript for this purpose there is a class Date which can be initialized in two ways:

1) var d1 = new Date(1979, 1, 6); - creates object with specified year, in the afternoon and month;

2) var d2 = new Date (); - creates object with the current date and time.

Inside the class Date stores time in milliseconds, we will use it and we will write the simple program by means of which it will be possible to learn the number of days about New year. The getTime function returns time in milliseconds, respectively, to receive a difference of dates, it is necessary to subtract one number from another and to share result on 1000*60*60*24 (the number of milliseconds in days). The code of the program and results of its performance are shown in drawing.

As addition readers are offered to modify a code so that it could work for any New year for what it is necessary to bring the value of year taken from the d1 variable in the d2 variable, the reference book on functions of the class javascript:Date can be found in the Internet.

On it we will finish the second part. As homework the following mathematical rebus published on one of the websites is offered to solve - to find the numbers hidden under symbols * for which the following condition of multiplication is satisfied: *** x * 2 * = ** 9*2 * .

Once again we will remind that if at implementation of the program the screen of the browser is empty, so in the program a mistake, and it is necessary to double-check the text.