function makeBattleship(message, board) { return message.split('').map(ch => { if(board.flat().indexOf(ch) < 0) { return ch; } else { const x = board.filter(row => row.indexOf(ch) >= 0).flat().indexOf(ch) const y = board.findIndex(row => row.indexOf(ch) >= 0) return [board[0][x], board[y][0]].join('') } }).join('') } console.log(makeBattleship( 'IGCTF{IMGOINGDOWNWITHTHESHIP}', [['Q', 'W', 'E', 'R', 'T'], ['Y', 'U', 'I', 'O', 'P'], ['A', 'S', 'D', 'F', 'G'], ['H', 'K', 'L', 'Z', 'X'], ['C', 'V', 'B', 'N', 'M']]))