All repetitions of values should be highlighted in the same color for the value. For example: if the number "2" occurs several times, all of them are highlighted with the same color (for example, green), if the number “5” occurs several times, then it is highlighted with a different color ( red) and so on. Numbers without repetitions remain black.
- And what does not work - to count the number of repetitions? - MBo
- No, select all repeating twos in green, all repeating fives in red, etc. I do not know how to implement - Tanya
- the numbers are given randomly and displayed in the table - Tanya
- oneWell, attach your spreadsheet and how you create numbers to a question, without your code, you can only write a very general answer - ThisMan
|
2 answers
Any task should be broken down into more or less elementary stages (rationally, decomposition).
In this case, we can offer this approach:
Создать словарь (map, dictionary), содержащий пары число-количество Пройти по ячейкам, для каждого числа посчитав количество повторов Создать массив или словарь, устанавливающий соответствие количество повторов - цвет Обойти ячейки, уже задавая цвет, соответствующий числу - most likely you do not need to bind the color to the number, but simply take it from the array - ThisMan
- Yes, it is reasonable. The number of repetitions is unlikely to go tightly so that the array indices directly correspond to them, so it may be better to have a dictionary again - MBo
|
const COLORS = ['tomato', 'pink', 'orange']; let table = document.querySelector('table'); let tds = table.querySelectorAll('td'); let tdInfoMap = Array.from(tds).reduce((map, td) => { let value = td.textContent; let info = map.get(value) || []; if (!info.length) { map.set(value, info); } info.push(td); return map; }, new Map()) Array.from(tdInfoMap.values()).forEach((tdAll, index) => { if (tdAll.length > 1) { tdAll.forEach(td => td.style.background = COLORS[index]) } }); <table> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> <tr> <td>d</td> <td>a</td> <td>b</td> </tr> <tr> <td>b</td> <td>e</td> <td>a</td> </tr> </table> - thank you very much - Tanya
|