From 737fea9cb1db61a6bd142ea29fd16a7177b53667 Mon Sep 17 00:00:00 2001 From: Krio Date: Fri, 3 Sep 2021 17:37:43 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- labs/lab2/desc.json | 7 +++++++ labs/lab2/main.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 labs/lab2/desc.json create mode 100644 labs/lab2/main.js diff --git a/labs/lab2/desc.json b/labs/lab2/desc.json new file mode 100644 index 0000000..620b797 --- /dev/null +++ b/labs/lab2/desc.json @@ -0,0 +1,7 @@ +{ + "number": "2", + "task": "Подсчитайте общее количество массивов внутри данного массива. Т.к. конвертирование строки в многомерный массив, не самая тривиальная задача, то исходный массив задается внутри кода.\narr = [[1, 2, 3, [1, 2, 3]], [[[]]], 3]", + "vars": [[]], + "scriptpath": "labs/lab2/main.js", + "complete": true +} \ No newline at end of file diff --git a/labs/lab2/main.js b/labs/lab2/main.js new file mode 100644 index 0000000..420a430 --- /dev/null +++ b/labs/lab2/main.js @@ -0,0 +1,22 @@ +"use strict"; + +function main(formName) { + let result = document.getElementById("result"); + let arr = [ + [1, 2, 3, [1, 2, 3]], + [[[]]], + 3, + ]; + + result.textContent = "Результат: " + countArr(arr); +} + +function countArr(item, level = 0) { + if (item instanceof Array) { + return (level > 0 ? 1 : 0) + + item.map( + (value, index) => countArr(value, level + 1) + ).reduce((a, b) => a + b, 0) + } + return 0; +}