Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active October 6, 2022 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/82ee2b46bdc2a9e916069a981912d873 to your computer and use it in GitHub Desktop.
Save McLarenCollege/82ee2b46bdc2a9e916069a981912d873 to your computer and use it in GitHub Desktop.
Count Surrounding Coins

Count Surrounding Coins

You are given a square board with coins ($) at certain positions.

Your task is to fill the empty boxes(' ') with appropriate numbers.

For every empty box you have to count the number of coins ($) present in the immediate surrounding blocks.

For example,

[[' ',' ','$'],
 [' ','$',' '],
 ['$',' ','$']]

should return

[[ 1 , 2 ,'$'],
 [ 2 ,'$', 3 ],
 ['$', 3 ,'$']]

CODE TEMPLATE


function countNeighbouringCoins(board){

 //write your code here
 
}
console.log(countNeighbouringCoins([[' ',' ','$'],
                                    [' ','$',' '],
                                    ['$',' ','$']]));
//should return 
//[[ 1 , 2 ,'$'],
// [ 2 ,'$', 3 ],
// ['$', 3 ,'$']]

console.log(countNeighbouringCoins([[' ',' '],
                                    [' ','$']]));
//should return 
//[[ 1 , 1 ],
// [ 1 ,'$']]

console.log(countNeighbouringCoins([[' ',' ','$',' '],
                                    [' ','$',' ',' '],
                                    ['$',' ','$',' '],
                                    [' ',' ','$',' ']]));
//should return 
//[[ 1 , 2 ,'$', 1 ],
// [ 2 ,'$', 3 , 2 ],
// ['$', 4 ,'$', 2 ],
// [ 1 , 3 ,'$', 2 ]]

Please watch this video for a hint for the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment