I’ve challenged myself to 30 days of codewars, one a day, time myself, and learn how I could have done it better.
Today I did: https://www.codewars.com/kata/chess-fun-number-1-chess-board-cell-color/javascript
Time: 10 minutes
What I’ve learnt: I knew I was going to have to check the same logic on both cells passed in, I thought creating an external function that would give me the color, then I can just call return checkColor(cell1) === checkColor(cell2)
.
Now onto the checkColor function! If the charCode of the cell[0] (the letter portion of the spot on the board) is even, we know that if the cell[1] (the number part of the spot on the board) is also even we are on “brown”, otherwise “white”, and the opposite is true if the cell[0].charCodeAt() is odd.
I used two ternaries, inside an if/else, and all was green. 🙂
const charCode = cell.toUpperCase()[0].charCodeAt() if (charCode % 2) return cell[1] % 2? "brown" : "white" else return cell[1] % 2? "white" : "brown"
What I learned from other people’s code:
I see a lot of people used a separate function to check the cell color like I did.
Someone used a bitwise operation `&1` which takes the number and converts it to Binary 1, which means if it’s odd it will return 1, and if it’s 0. I don’t totally get it, and probably won’t use it at this point, but at least I will recognize it. More info is here.
Other than that, it seems everyone used similar logic to mine.
Can’t beleive there are only 3 days left! 3…2…1!