Everyone Can Code!

JavaScript Kata #1: Square(n) Sum


If you missed the introductory post, it's here.

Just to get this out of the way, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.

Kata #1

DESCRIPTION:
Complete the square sum function so that it squares each number passed into it and then sums the results together.

For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9.

Starting code:

function squareSum(numbers){

}

Going into this problem, I only started to learn about arrays and thought immediately about them because the example explicitly said '[1, 2, 2]'. I learned from the bootcamp that there are methods you can apply to keywords. As you may well know, I am a beginner so I had to refer to one of the recommended (free book) readings: The JavaScript Way by Baptiste Pesquet since I haven't memorized the methods yet. Since we don't know how many numbers will be passed to the function squareSum's array parameter when it will be called, I thought I could use the .length method and iterate through each array element then square it and add the product to a variable. Here was my first attempt:

function squareSum(numbers){
  let sum = 0
  for(i = 0 ; i < numbers.length; i++){
    sum += (numbers[i] * numbers[i])    
  } 
  return sum
}

But for some unknown reason, it won't accept this solution so I tried another loop:

function squareSum(numbers){
  let sumOfNumbers = 0
  let i = 0
  while (i < numbers.length){
    sumOfNumbers += (numbers[i] ** 2)
    i++
  }
   return sumOfNumbers
}

And then it worked! Solutions can be tested before submission so when the tests worked, I promptly submitted it to gain my points. In the following page, users are also provided how other people approached and solved the problem, and the solution with the highest votes are tagged as "Best Practices". Here is the solution with the highest votes:

function squareSum(numbers){
  return numbers.reduce(function(sum, n){
    return (n*n) + sum;
  }, 0)
}

Which is nice because I learned about a new array method called .reduce. Had to read up on it in another recommended reading to know more about it and understand it fully though.

I kept wondering what went wrong with my first solution and I was thinking, "Did I use a keyword as a variable?" or "Doesn't Codewars accept lines of code without semicolons?" (more on that semicolons thing here) so I kept scrolling down with all the best practices then lo and behold:

A variation of my solution

Ranked #4 in Best Practices was my initial attempt, with some slight variation in variable declaration plus semicolons per line. 

(-_-) Okay. Probably a fluke. (╯°□°)╯︵ ┻━┻ *flips table*

🤣 Oh well. ¯\_(ツ)_/¯

What's important is I was able to solve it with another solution and my logic was sound. What's funny is that before posting this, I went back and redid the kata still with my first solution and then it magically accepted it. Codewars people are probably like:

Magic!

(For some reason, GIFs won't embed or play when uploaded so this will have to do 🤣)

So that's it for Kata #1, stay tuned for more katas to be solved!

P.S. I recently found out that Codewars apparently has a referral link which allows users to gain honor points, here is mine if you would like to try this site out for yourself. It's no big deal, just some in-house point system for in-site privileges related to user experience:

Codewars Privileges

As I am a beginner navigating the site: if I gain Honor points I can probably help future beginners as I can vote for things that can help them or make the site easier to use. If you prefer the referral-free link anyway, it's here.

How do you rate this article?

2


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.