Solving a Codewars Challenge as a Beginner using JavaScript.

ยท

3 min read

๐—–๐—ผ๐—ฑ๐—ฒ ๐—–๐—ต๐—ฎ๐—น๐—น๐—ฒ๐—ป๐—ด๐—ฒ: ๐’๐ฉ๐ข๐ง ๐€๐ซ๐จ๐ฎ๐ง๐, ๐“๐จ๐ฎ๐œ๐ก ๐ญ๐ก๐ž ๐†๐ซ๐จ๐ฎ๐ง๐

The Challenge: Given a list of directions to spin, "left" or "right", I had to return an integer of how many full 360ยฐ rotations were made. Each word in the array represents a 90ยฐ rotation in that direction.

Example:
["right", "right", "right", "right", "left", "right"] โžž 1

  • I spun right 4 times (90 * 4 = 360)

  • Then, I spun left once (360 - 90 = 270)

  • Finally, I spun right again, completing a full rotation (270 + 90 = 360)

My Thought Process:

  1. For a right turn, add 90. For a left turn, subtract 90.

  2. I needed a way to iterate through the array and consider each turn.

  3. Calculate the total degrees turned and store it in a variable.

  4. Identify a mechanism to determine how many full 360ยฐ rotations were achieved.

With that in mind, I started the challenge here is the code provided by codewars

function spinAround(turns) {
// Here be dragons!
  return 0;
}

First I needed to declare a new variable to store the calculations happening when iterating inside the array.

Initialized with zero as the starting value as we add and subtract from it.

let total = 0;

I decided to use the forEach method for iteration:

turns.forEach(turn =>{})

At this point, I needed to loop through the array and add 90 if it's right and minus 90 if it's left hence I decided to use an if-else statement

if(turn === 'right'){
    total += 90;
} else if(turn === 'left'){  
    total -= 90;
}

Here is how the code looked after the statement.

function spinAround(turns) {
// Here be dragons!
    let total = 0;
    turns.forEach(turn =>{
        if(turn === 'right'){
        total += 90;     
        } else if(turn === 'left'){  
        total -= 90;    
        }
        })
    return 0;
}

Here is where I had a challenge that I needed to figure out. I had the total degrees turned and if I divided by 360 to find the amount of times the turn happened it would leave some remainder. At first, I used Math.floor to find the whole value without the decimal But the problem with that was if there were more left turns than right turns it would return a negative value which we don't want. I ended up googling methods of converting a negative value to a positive and I found Math.abs - for finding the absolute value. With a combination of Math.floor and Math.abs, I was able to return the number of turns by dividing by 360.

return Math.floor(Math.abs(total/360));

Here is the full solution

function spinAround(turns) {
    // Here be dragons!
    let total = 0;
    turns.forEach(turn =>{
      if(turn === 'right'){
      total += 90;     
      } else if(turn === 'left'){  
      total -= 90;     
      }
    })
    return Math.floor(Math.abs(total/360));
}

Here is what I learned after that challenge:

  1. Breaking the challenge into smaller parts makes it look easier to solve and understand what is needed to solve it.

  2. Writing the solution in plain text helps me understand how I will implement the code.

  3. The challenge looked daunting before I started attempting and after some minutes of trying it ended up feeling I could complete the challenge.

  4. I need more practice to force my brain to think in different ways. I will try to solve the one challenge every day.

  5. I kinda love solving things now ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

I would appreciate any criticism and advice I could get. feel free to contact.

Thanks in advance.

ย