Solving my first codewars challenge

Solving my first codewars challenge

Table of contents

No heading

No headings in the article.

Codewars is something that I was putting off doing for a while once it was introduced in the program I am following. I am in the 100 devs agency where they help people affected by the pandemic gain programming skills and help get paid clients. This program goes through the fundamentals of HTML, CSS, JavaScript, API integration and Node.js for backend development. I have been learning JavaScript through this program since March of this year and the instructor, Leon, mentions daily coding challenges in order to lock in the different methods and logic being implemented in the code that we follow during class time. I was hesitant about this because I was not fully confident in my JavaScript skills just yet, and I imagined coding challenges to be for people more advanced than me.

I quickly found out that was not the case. I signed up for codewars, linked my Github account and added the 100 devs clan. When I got it all set up I looked at my first 8kyu challenge and kind of froze up and did not know how to approach it at first. The next day I took another look at it and read through what the challenge was asking me, which was complex but at the same time manageable. Here is the prompt:

Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.

Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren't.

Ok so, breaking this down I knew there were a couple steps I had to complete first:

  • Write a function

  • Check if one flower has an even number of petals and the other has an odd number of petals using comparison

  • Check if the petals are even or odd using modulo

  • Return true if both are true, otherwise return false

  • Store value in a variable and pass in as an argument

  • Check solution and compare

After laying out all of the steps the problem was asking for it was easier to start forming my solution, so here is what I came up with: WARNING I’m a baddie and this is bad code

function lovefunc(numOfPetals){
                if (numOfPetals%2==0&&numOfPetals%2==0) {
                    return true;
                }else return false;
            }

This was the right approach, but there are a few things wrong with my solution that I realized after looking at the solution. First, my parameters, I needed two parameters for each flower. Then the comparison, I needed to check if one of the flowers was not equal to 1, to check for odd, or not equal to 0, to check for even. I was surprised when I saw the different approaches that were a lot more simplified. There was one that was similar to my approach:

function lovefunc(flower1,flower2){
                if (flower1%2==0&&flower2%2!==0) {
                    return true;
                }else if(flower1%2!==0&&flower2%2==0){
                    return true;
                }else return false;
            }

In this solution there are two parameters for each flower and the first conditional statement is checking if the first flower has an even number of petals and the second is checking if the second flower does not have an odd number of petals. The second conditional is checking the same but switched, if the first flower does not have an even number of petals and the second has an odd number of petals. Then, if both of those statements are not true then the else statement, return false, will run.

This first problem was actually really fun to think through and compare my solution to the correct one and learn from the mistakes that I made while solving it. I have been training in codewars for a little over a month now, and I’m hooked! I recently leveled up to a 7kyu and am trying my best to do them daily. If there is any advice I would give to someone first starting out with codewars is to just try it for at least 20 minutes, give it your all, and then look at the solution if you don’t pass the tests. Also, if you can, find a community to do it with! I recommend the 100 devs community, you can find them on YouTube, Discord and Twitch!