2.23 codewars!

4.5h Today I did a bunch of code challenges. I like the feeling of sloooooowly getting better at this. I know the only way to get better at it is to do it as well. I shifted back to codewars tonight (from hackerrank last night) because I like being able to have more functionality in the browser ide, and I only have one function to work on, as opposed an entire program that will need to sometimes be read for context. I’m sure they are both helpful, and I’ll probably flip flop from one to the other fairly often.

I also spent a ton of time troubleshooting why my nest couldn’t connect to the wifi. Long, long story short: when the battery gets low it won’t connect to wifi at all as a power saving mode. It might get low because you haven’t been using the furnace lately and you don’t have a “c-wire” or constant power wire. If you’re lucky like me you can dig into your wall ever-so-stealthily while your family sleeps and find the right wire so you can hook it up. In the end it was a success, but it was not the wifi config related fix that I thought it might be.

I’ve got one more kata that I’m still working on. It has taken far more time than the other two and I’m close, but not finished on it yet. Sleep calls me. The kata can wait until tomorrow.

Tic-Tac-Toe Checker (5 kyu)

This kata was about checking a tic tac toe game and determining if either player had won, if it was a tie, or if it was still in progress. I wish I could have made it more procedural (ie I had to hard code in the column and row values for the diagonals) but it worked in the end. I’ve got a similar problem in my head where I need to implement strategy, and doing this one didn’t give me as many ideas on how to do that as I would have liked.

function isSolved(b) {
  for (let r = 0; r < 3; r++){
    if (b[r][0] === b[r][1] && b[r][0] == b[r][2] && b[r][0] !== 0) {
      return b[r][0] //== 1 ? "X" : "Y" //"row match" //
    } 
  }
  for (let c = 0; c < 3; c++){
    if (b[0][c] === b[1][c] && b[1][c] == b[2][c] && b[0][c] !== 0) {
      return b[0][c] //== 1 ? "X" : "Y" //"column match" //
    } 
    if ((b[0][0] === b[1][1] && b[1][1] == b[2][2] && b[0][0] !== 0)) {
      return b[0][0] //== 1 ? "X" : "Y" //"diagonal match" //
    }  
    if ((b[0][2] === b[1][1] && b[1][1] == b[2][0] && b[0][2] !== 0)) {
      return b[0][2] //== 1 ? "X" : "Y" //"diagonal match" //
    } 
  }
  for (let r = 0; r < 3; r++){
  console.log(b[r])
    if (b[r].includes(0)) { //test for empty spots to rule out a draw
      return -1
    }
  }  
  return 0 //it must be a draw
}

greed-is-good (5 kyu)


This kata is about keeping track of multiple dice rolls and then scoring based on a set of rules. I converted the dice rolls input from an array to an object where the number was the key and the value was the frequency. The calcScore function then calculated the score. This was relatively simple logic. I spent more time getting it short than I did making it work. One gotcha I ran into while doing it was that I tried passing the array directly into the .map function. like this:

[2,3,4,5,6].map(roll => {
  if (countHash[roll] > 2) {
    countHash[roll] -= 3
    score += (roll*100)
  }
})

That gave me an error, while assigning the array to a variable and then calling .map on it ran as expected. Both ways work fine in the chrome dev tools though. Why? Oh. I think it’s because codewars must be running it in strict mode. Hmm. I don’t know much about that, but I tried adding a semicolon to the previous line AND IT WORKED! What a weird glitch.

let returnHash = (dice) => {
  countHash = {'1':0,'2':0,'3':0,'4':0,'5':0,'6':0}
  dice.map((each) => {
      countHash[each] += 1
  })
  return countHash
}

let calcScore = (countHash) => {
  let score = 0 
  if (countHash[1] > 2) {
    countHash[1] -= 3
    score += 1000
  };
  [2,3,4,5,6].map(roll => {
    if (countHash[roll] > 2) {
      countHash[roll] -= 3
      score += (roll*100)
    }
  })
  score += ((countHash[1]*100) + (countHash[5]*50))
  return score
 }
 
function score( dice ) {
  return calcScore(returnHash(dice))
}

Tell me what you think.

This site uses Akismet to reduce spam. Learn how your comment data is processed.