Hello everyone! Here is link to the problem: https://www.codewars.com/kata/56ff6a70e1a63ccdfa0001b1/train/javascript

I have squared all elements of the first array, and I have squared all elements of the second array. Then, I added up the sum in each array. I then am stating "if sumOfResultOne is greater than sumOfResultTwo, then return ‘true’. Else if the sumOfResultTwo is greater than sumOfResult One, then return ‘false’. I don’t see why this code is not working ):

``````function arrayMadness(a, b) {
let resultOne = a.map(x => x ** 2);
console.log(resultOne);

let resultTwo = b.map(x => x ** 2);
console.log(resultTwo);

const sumOfResultOne = resultOne.reduce((acc, cur) => acc + cur, 0);
const sumOfResultTwo = resultTwo.reduce((acc, cur) => acc + cur, 0);

if (sumOfResultOne > sumOfResultTwo) {
return true;
} else if (sumOfResultTwo > sumOfResultOne) {
return false;
}
}
``````

NVM! Result two is supposed to be cubed. Changed code to this and it worked

``````function arrayMadness(a, b) {
let resultOne = a.map(x => x ** 2);
console.log(resultOne);

let resultTwo = b.map(x => x ** 3);
console.log(resultTwo);

const sumOfResultOne = resultOne.reduce((acc, cur) => acc + cur, 0);
const sumOfResultTwo = resultTwo.reduce((acc, cur) => acc + cur, 0);

if (sumOfResultOne > sumOfResultTwo) {
return true;
} else if (sumOfResultTwo > sumOfResultOne) {
return false;
}
}
``````
1 Like

^ Doesn’t seem to be part of the requirements, not that it should stop you from thinking about it.

Wouldn’t it be easier to just have the two `reduce` calls and put all the math inside them? I mean it just seems like an excessive amount of looping.

I’m trying to get to the point where I can solve the problems. Then, after I am able to pretty much solve any problem, “tweak” the code to make it like the way you said. This is just the only way I could think of.

That is a perfectly fine approach. I’m just saying that the math performed in the `map()` fits quite well inside the `reduce()`.

I know `reduce()` is usually taught to beginners using addition (which is fine) but just make sure that doesn’t limit you to all the options it really gives you. It is a powerful method and only using it for addition would be a shame.

In this case, what I’m suggesting isn’t really much different than using it for addition, just with some extra math tacked on to the addition.

I wish I were able to think more on my feet with the built-in JavaScript functions like reduce(). It’s just very abstract and I’ve only learned to do it for addition, subtraction, etc etc lol. but I had it wrong because I was doing ** 2 and not ** 3 for a cube lol

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.