Created
October 15, 2017 11:45
-
-
Save unilecs/be04395d8d3993b7d2b7bc7e69d4429f to your computer and use it in GitHub Desktop.
Преобразование массива путем произведения всех значений
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getMultipleElementsInArray(arr) { | |
// результирующий массив произведений | |
let resultArr = []; | |
// произведение всех значений до текущего | |
let multiple = 1, i = 0; | |
while (i < arr.length) { | |
resultArr[i] = multiple; | |
multiple *= arr[i]; | |
i++; | |
} | |
// произведение всех значений после текущего | |
// также мы вычисляем текущее результирующее значение | |
multiple = 1, i = arr.length - 1; | |
while (i >= 0) { | |
resultArr[i] *= multiple; | |
multiple *= arr[i]; | |
i--; | |
} | |
return resultArr; | |
} | |
const arr = [2, 4, 3, 5]; | |
console.info(getMultipleElementsInArray(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment