Solving a Codewars Challenge as a Beginner using JavaScript.
๐๐ผ๐ฑ๐ฒ ๐๐ต๐ฎ๐น๐น๐ฒ๐ป๐ด๐ฒ: ๐๐ฉ๐ข๐ง ๐๐ซ๐จ๐ฎ๐ง๐, ๐๐จ๐ฎ๐๐ก ๐ญ๐ก๐ ๐๐ซ๐จ๐ฎ๐ง๐
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:
For a right turn, add 90. For a left turn, subtract 90.
I needed a way to iterate through the array and consider each turn.
Calculate the total degrees turned and store it in a variable.
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:
Breaking the challenge into smaller parts makes it look easier to solve and understand what is needed to solve it.
Writing the solution in plain text helps me understand how I will implement the code.
The challenge looked daunting before I started attempting and after some minutes of trying it ended up feeling I could complete the challenge.
I need more practice to force my brain to think in different ways. I will try to solve the one challenge every day.
I kinda love solving things now ๐๐๐
I would appreciate any criticism and advice I could get. feel free to contact.
Thanks in advance.