'How to get the next element in an array with recursion?
I want to loop through an array with a recursive function, but I'm kinda stuck on how to do it.
What I want to achieve is this:
- Get the first two arrays in
numbersArray. - Get the first numbers in both arrays and add these together and then divide by 10.
- Push the outcome into
sumOfNumbers. - Get the next set of numbers and in
numbersArrayand repeat until all the numbers have been calculated. - Get the next set of two arrays in
numbersArrayand repeat.
This is what I have now. It only gets the first element of an array:
function unitTest(block1, block2, result = []) {
let sumOfNumbers = []
let numbersArray = [
[9, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
]
block1 = numbersArray[0][0]
block2 = numbersArray[1][0]
sumOfNumbers = (block1 + block2 ) % 10
result.push(sumOfNumbers)
console.log(sumOfNumbers)
console.log(result)
if (result.length == 10) {
return result
}else{
// unitTest()
}
Solution 1:[1]
In recursion you need a base case and a recursive case. I put the whole array as a parameter and an index to know what item in the array we are processing. The stop recursion occurs when that index is greater than array. In recursive cases, we take 2 arrays and do the operations. Then, we call recursive the 2 next blocks
function unitTest(numbersArray, arrayIndex, result = []) {
if (arrayIndex + 1 < numbersArray.length) {
var block1 = numbersArray[arrayIndex]
var block2 = numbersArray[arrayIndex + 1]
for (var i = 0; i < block1.length; i++){
sumOfNumbers = (block1[i] + block2[i]) % 10;
result.push(sumOfNumbers);
console.log(sumOfNumbers);
}
unitTest(numbersArray, arrayIndex + 2, result);
} else {
console.log(result);
}
}
let sumOfNumbers = []
let numbersArray = [
[9, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
]
unitTest(numbersArray, 0)
Solution 2:[2]
from what I understand, you are trying to compute the sum % 10 of each element of the array 0-1, 2-3, 4-5, ...
The usage of the recursion here is not necessary and a simple loop will do the job.
Here is the solution I could suggest
const numbersArray = [
[9, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
[9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 7]
];
const results = [];
for (let i = 0; i < numbersArray.length - 1; i += 2) {
const result = numbersArray[i].map((value, index) => {
return (value + numbersArray[i + 1][index]) % 10;
});
results.push(result);
}
console.log(results);
The results constant is of type number[][] which contains the arrays of the computation for each pair of the original array.
Hope that will help you :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Victor |
| Solution 2 | Adrien De Peretti |
