diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..1b7c5ce 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,3 +1,48 @@ -function countIslands(grid) {} +function countIslands(grid) { + //This function directs every activities that goes on here. it calls the checkDreyLand function, + //which searches for inland, gives it the axis of the point to start seach from and collects + //feedback from it. + function manager(matrix){ + let inlandRecorder=0; + for(let row=0; row=0 && matrix[axisX-1]!==undefined && matrix[axisX-1][axisY]==1){ + matrix[axisX-1][axisY]=0; + checkDryLand(matrix,axisX-1,axisY); + } + //checking adjacent left + if(axisY-1>=0 && matrix[axisX][axisY-1]!=undefined && matrix[axisX][axisY-1]==1){ + matrix[axisX][axisY-1]=0; + checkDryLand(matrix,axisX,axisY-1); + } + //checking adjacent right + if(axisY+1<=matrix[0].length && matrix[axisX][axisY+1]!=undefined && matrix[axisX][axisY+1]==1){ + matrix[axisX][axisY+1]=0; + checkDryLand(matrix,axisX,axisY+1); + } + return "Thank you"; + } + //the manager is done and about to return result to the company owner(i.e countIsland). Always remember + //that the compant owner(countIsland) reports it the creator. Which is YOU... + return manager(grid); +} + module.exports = countIslands; diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..5b9f766 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -1,3 +1,81 @@ -function wordSearch(words, word) {} +function wordSearch(words, word){ + //this function searches for all the index positions(rows and columns) of the first character + //of the "word" and store them as two sets of arrays(rowIndex and colIndex) inside an object + //and return the object. These positions are the starting points to searching from. + function searchIndex(words,word){ + let rowIndex=[]; + let colIndex=[]; + + for(let row=0; row=0 && words[row-1]!==undefined && words[row-1][col]==word[indexPositionsOfWord]){ + words[row-1][col]=0 + if(checkNextChar(words,word,(row-1),col,indexPositionsOfWord+1)){ + return true; + } + + } + if(col-1>=0 && words[row][col-1]!=undefined && words[row][col-1]==word[indexPositionsOfWord]){ + words[row][col-1]=0 + if(checkNextChar(words,word,(row),col-1,indexPositionsOfWord+1)){ + return true; + } + + } + if(col+1<=words[0].length && words[row][col+1]!=undefined && words[row][col+1]==word[indexPositionsOfWord]){ + words[row][col+1]=0 + if(checkNextChar(words,word,(row),col+1,indexPositionsOfWord+1)){ + return true; + } + } + return 0; + } + return manager(searchIndex(words,word)); +} module.exports = wordSearch;