Hello. Make the task in which you want each number from the array output in sootvetstvuyuschuyu cell in the table. Total cells 9, and numbers much more. For example, in the array there are numbers 2,1,4,3,6,8. What numbers can be repeated. See 2-ku - put in cell number 2.. 1-MC in cell 1, and so on.

I have an array of variables 1 to 9

These variables are needed in order to check the number from the array and display them in a table cell(td). For example, like this:

There are 2 variables a=42, b=79. Their meaning can be different, but they double digit. And I need to separate them so that they become unique(4,2,7,9). Then the values of these variables enter into the array massiv[] using the function:

Now the essence of the question: how can I verify the last 4 digits in the array (in our example, these four digits are variables and a,b(4,2,7,9) ) and record them in a certain cell in the table in brackets? For example, in our cell there are already 2 here, and since the last 4 numbers of array 2 is found on, then put it in brackets (2) to obtain 2(2) ? So I deduce variables:

For clarity, a living example - https://jsfiddle.net/e1c81ujg/

I have an array of variables 1 to 9

```
var massiv = [];
var fkol_1 = 0;
var fkol_2 = 0;
---------
var fkol_9 = 0;
```

These variables are needed in order to check the number from the array and display them in a table cell(td). For example, like this:

```
if ((massiv[i] == 1)) {fkol_1++;}
if ((massiv[i] == 2)) {fkol_2++;}
//and so on up to 9
```

There are 2 variables a=42, b=79. Their meaning can be different, but they double digit. And I need to separate them so that they become unique(4,2,7,9). Then the values of these variables enter into the array massiv[] using the function:

```
function splitNumber(number) {
number.toString().split(").forEach(number => massiv.push('('+number+')'));
}
splitNumber(a);
splitNumber(b);
```

Now the essence of the question: how can I verify the last 4 digits in the array (in our example, these four digits are variables and a,b(4,2,7,9) ) and record them in a certain cell in the table in brackets? For example, in our cell there are already 2 here, and since the last 4 numbers of array 2 is found on, then put it in brackets (2) to obtain 2(2) ? So I deduce variables:

```
<p>'+fkol_1+'</p>
<p>'+fkol_2+'</p>
/* and so on to fkol_9' */
```

For clarity, a living example - https://jsfiddle.net/e1c81ujg/

asked July 9th 19 at 13:09

1 answer

answered on

Solution

Sorry, but it's unreadable.

replace at least

and so on, no need to write 10 lines in operation that with the right approach it is possible to write a single line.

And naming variables, try to name them clearly and in English

```
if ((massiv[i] == 1)) {fkol_1++;}
if ((massiv[i] == 2)) {fkol_2++;}
//and so on up to 9
```

replace at least

`fkol[massiv[i]]++;`

and so on, no need to write 10 lines in operation that with the right approach it is possible to write a single line.

And naming variables, try to name them clearly and in English

Find more questions by tags jQueryHTMLJavaScriptCSS

if ((massiv[i] == i )) { fkol[massiv[i]]++ }

If you go by the "living example", you'll see that I vovtroy table with variables fkol_1, etc. As in this case, I return the value? - nikita.Stracke commented on July 9th 19 at 13:14

fkol_1 replaced by fkol[1] and so on. - antonetta.Collins commented on July 9th 19 at 13:20

In General, if I understand correctly your question, here's a bit updated fiddle https://jsfiddle.net/e1c81ujg/3/

in General your code style is saved, so questions should arise.

But in General: take more time to study the culture code and implementation logic of your programs - antonetta.Collins commented on July 9th 19 at 13:32

Here is your updated fiddle - https://jsfiddle.net/e1c81ujg/5/ I changed the condition in if. There you have 9, but I put 16 as the original condition counts the number of repeated numbers and displayed in a table. If you take the online example www.astrocentr.ru/index.php?przd=pifthere displays the same output as yours, but corrected by me.

Now regarding the issue: https://jsfiddle.net/e1c81ujg/4/ in my example (slightly modified) in the fourth cell add up the numbers between itself and displays their sum: let's Say, 11111 2 3 will be equal to 10, as 1+1+1+1+1+2+3. Therefore, the top 10 recorded in the fourth cell. And so for each row(TR). You have a 4 cell(td) in your example (https://jsfiddle.net/e1c81ujg/5/) oddly finds and displays 0. How to make the numbers NOT in parentheses were considered only among themselves, and the numbers in brackets are only among themselves.

At the end of a JavaScript is shown as is the sum of the 4th cell. Tell me - nikita.Stracke commented on July 9th 19 at 13:35

I even generally don't understand where you add up some numbers with others.

The numbers in brackets 16,17,18,19 are stored in the array indices, the sum by rows we can calculate something like this:

var str1 = 0;

var str2 = 0;

var str3 = 0;

for (var i = 16; i < 20; i++) {

if (massiv[i] < 4) {

str1 += massiv[i];

} else if (massiv[i] < 7) {

str2 += massiv[i];

} else {

str3 += massiv[i];

}

}

so str1 will contain the sum of cells 1,2,3

in str2 will contain the sum of cells 4,5,6

in str3 is stored the sum of the cells 7,8,9 - antonetta.Collins commented on July 9th 19 at 13:38

I think the question was answered in some detail, and I deserve a tick ;)

Otherwise, it is necessary to formulate a question, a La: "decide for me laboratory work" - antonetta.Collins commented on July 9th 19 at 13:41