Codewars: Square(n) number (8 kyu) TDD in JavaScript

Hi! Welcome to Codewars. In this series I
gonna solve TDD katas using javascript. I’m Anatol and you’re
watching the Good Parts of the front-end development. If this is your first
time here start now by subscribing. Let’s solve today kata titled “Square(n)
Sum.” Complete the square sum method, so that it squares each number passes
into it and then sums the results together. Okay. I have an array and then I need to square each number. A “map” method is a
good fit for that. So, let’s define a result that will be 0 and then a new array. I will… “numbers” map over it and for each
element return it’s squared. Let’s check in console what we have.
Yes, as you see each element is squared. Now I need to sum all the elements. So, I
can use for that “forEach” method. “arr” “forEach”, then each element… I just add
that element to our result and then return a result value. Let’s check. Yes,
it works. However, we have a method that fits better, especially for this purpose.
It’s a “reduce” method. Let’s use it. I will return numbers and reduce.
“accumulator” and “current” element. Then “accumulator” add “current element” square.
And I’ll start from zero. Let’s check. Yes, that’s correct.
And make a compact solution. using an “arrow function.” Let’s try.
Yes that’s correct. My code is available on GitHub. Documentation is available in the Internet. Follow the link if you’re eager to get more. If you like this video give it “thumbs up”, share it with your friends, subscribe to the channel and watch other episodes. Thanks for
watching and dive deeper.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *