I’m trying to solve this problem, but my code does not work properly so here is the problem that I’m trying to solve.
Write a function addingAllTheWeirdStuff which adds the sum of all the
odd numbers in array2 to each element under 10 in array1. Similarly,
addingAllTheWeirdStuff should also add the sum of all the even
numbers in array2 to those elements over 10 in array1.If any element in array2 is greater than 20, add 1 to every
element in array1.
And here is my code.
function addingAllTheWeirdStuff(array1, array2) {
let arr1_2 = []
let totalOds = 0
let totalEvens = 0
// suming th odds numbers in array2
totalOds = array2.reduce((acc, curr) => (curr % 2 !== 0 ? acc += curr : null, acc), 0)
// suming th even numbers in array2
totalEvens = array2.reduce((acc, curr) => (curr % 2 === 0 ? acc += curr : null, acc), 0)
// adding the total to odd number in array1
for (let i = 0; i < array1.length; i++) {
let arods = array1[i]
if (arods % 2 !== 0 && arods < 10) {
arods += totalOds
arr1_2.push(arods)
}
}
// adding the total to even numbers in array1
for (let j = 0; j < array1.length; j++) {
let areven = array1[j]
if (areven % 2 === 0 && areven > 10) {
areven += totalEvens
arr1_2.push(areven)
}
}
return arr1_2
}
console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15], [1, 2, 3, 4, 5]));
// expected log [10, 12, 14, 23, 21]
console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15, 1], [1, 2, 3, 4, 5, 22]));
// expected log [11, 13, 15, 46, 44, 11]
2
Answers
Instead of adding the sums of odd and even numbers your code adds it individually, I’ve made necessary changes to the code and it shall work
By having a little hole for values equal
10
, according to the specs:Take a first iteration for getting a bonus for a greater value and sum even and odd values.
Later return a new array with even/odd or no offsets.