The tests were written so today was finally about coding! I worked on writing the ship object methods and properties to ensure that they pass all the tests. There was definitely something satisfying about passing each test. It felt like I was making tangible progress even though I was working on passing simple unit tests. If I get the hang of TDD it might help me in the future but my struggles with it so far cannot be overlooked. I hope I can speed up with more practice. The process continues though.
TLDR;
Okay, so here are the highlights of what I did:
- JavaScript -> Re-read the
Jestdocs on theexpect().toThrow()API method. My last test was failing because I had forgotten that you need to use a callback function as an argument within theexpectfunction to have the error register successfully. - Practice -> Continued working on the Battleship project. Got all of my initial tests for the
Shipfactory passing. Next is to try my had at the game grid factory. Good luck to me LOL.
Ship Factory
export default function constructShip(length, coordinates = []){
coordinates = coordinates.map(posArr => ({
x: posArr[0],
y: posArr[1],
isHit: false,
}))
return {
getLength: () => length,
hit: (x,y) => {
for(let i=0; i < coordinates.length; ++i){
const pos = coordinates[i]
if(pos.x === x && pos.y === y){
pos.isHit = true
return true;
}
}
return false
},
isSunk: () => {
for(let i=0; i < coordinates.length; ++i){
const pos = coordinates[i]
if(!pos.isHit)
return false
}
return true
},
isHit: (x,y) => {
for(let i=0; i < coordinates.length; ++i){
const pos = coordinates[i]
if(pos.x === x && pos.y === y){
return pos.isHit ? true : false
}
}
throw new Error('[x,y] coordinates provided do not match an existing position on the ship.')
},
isCoordinatesSet: () => coordinates.length > 0 ? true : false,
setCoordinates: (arr) => {
if(coordinates.length > 0)
throw new Error('Ship Coordinates have already been set');
coordinates = arr.map(posArr => ({
x: posArr[0],
y: posArr[1],
isHit: false,
}))
},
}
}
Goal For Round 7 of the #100DaysofCode Challenge
This is my seventh round of the “#100daysofcode” challenge. I will be continuing my work from round five and round six into round seven. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in turn derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.
I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.