Challenge #13 - Not producing correct result


#1

I’m getting the expected output but when submitting I am getting the message ‘Your code ran but did not produce the correct result.’

What am I missing?

function allRocks() {
const rowCount = countRows();
const columnCount = countColumns();
let coordinates = [];

for(let i = 0; i < rowCount; i++) {
GRID[i].forEach((elem, index) => {
if (isRock(letterArray[index] + (i+1))) {
coordinates.push(letterArray[index] + (i+1));
}
})
}
return coordinates;
}

function allCurrents() {
const rowCount = countRows();
const columnCount = countColumns();
let coordinates = [];

for(let i = 0; i < rowCount; i++) {
GRID[i].forEach((elem, index) => {
if (isCurrent(letterArray[index] + (i+1))) {
coordinates.push(letterArray[index] + (i+1));
}
})
}
return coordinates;
}


#2

Does anyone have any insights into this? I’ve read most of the Challenge #13 posts but haven’t been able to pin down my issue.


#3

Is letterArray a global variable? There is no reference to it in your copy&paste code.


#4

yes I have it as a global variable currently.
const letterArray = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’];


#5

It is working in my testing. Technically the forEach loop should have a semi-colon at the end. Some environments require the semi-colon, some don’t. But for a quick test put a semi-colon after the closing bracket of both forEach loops and try it. I don’t think the testing environment is that sensitive, but worth a quick try.

GRID[i].forEach((elem, index) => {
if (isRock(letterArray[index] + (i+1))) {
coordinates.push(letterArray[index] + (i+1));
}
}); <—

I’ll keep looking too.


#6

That didn’t fix it. I hugely appreciate your help :grinning:


#7

Can I see your countRows and countColumns?

Sorry, Im grasping at straws, because the code is giving me the correct answer too.

Also some people said that when they copy and paste code into the lighthouse labs environment it changes the quote style to a fancy quote, which is not accepted for testing. Are all of the quotes around your letterArray normal quotes.

And triple check that letterArray code you are submitting to lighthouse is global, as your functions require it.


#8

Also try logging out and logging back in and submitting again. People have mentioned that works for them.

Copy down your code first though so you don’t loose it.


#9

function countRows() {
return GRID.length;
}

function countColumns() {
return GRID[0].length;
}

I just put all my quotes as double quotes and tried re-logging in. No success yet.


#10

Sorry, I’m stumped.

The only other thing I would try, is declaring letterArray inside each function, but shouldn’t really matter, because I’m sure some of your other functions require the global letterArray.


#11

Thanks for taking the time to look through this with me. I’m stumped too.


#12

Hello, I modified the GRID to be 10 rows times 11 columns and your code returned an error. I haven’t figured out which part of your code is causing the error. Will let you know if I figure it out.


#13

Oh geez that was my problem. I thought I had fixed that but it was lost during my tweeks to get the code to run. Than you!


#14

Can you share with us which part of your code caused the error?


#15

I think the problem causing his code fail is because of the function for converting coordinate(e.g ‘B10’) to [9][2].
His allRocks() produce result: ["D1", "E3", "F3", "E4", "F4", "B8", "H8", "B9", "D10"]
The correct result should be ['D1', 'E3', 'F3', 'E4', 'F4', 'B8', 'H8', 'B9', 'B10']

It is very possible he used some approached for previous challenges like:

function convertRow(coordinate) {
   return coordinate.charAt(1) - 1;
 }

Above function will cause undesired behavior when converting double digit row number.
probably result in ‘B10’ => [0][1] == '' but ‘D10’ => [0][4] == '^'.

@randomint @userName1


#16

owenip is correct. The code for pulling the row from the input was set for only 1 digit so once the rows went above 9 the code wouldn’t work. I had fixed it earlier but I must have undone it during the debugging process.


#17

Ahh! Glad to have @owenip help identify the problem!


#18

Hi can you help with the code below

function countRows() {

 return GRID.length;

}

function countColumns() {

 return GRID[0].length;

}

function gridSize() {

let width = countColumns();
let height = countRows();

return width + ’ x ’ + height;

}

function totalCells(){

totalC = countColumns() * countRows();

return totalC;
}

function convertColumn(col){

var alpha = ['A','B','C','D','E','F','G','H','I','J','k','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

        
      for (var i = 0; i < alpha.length; i++){
        
         if (col.charAt(0) == alpha[i]){
           
            return  alpha.indexOf(col.charAt(0));
         }
        
      }  

}

function lightCell(col){

            var newRows =[];

            for (var i = 0; i < GRID.length; i++){
              
               newRows[i + 1] = GRID[i];
              if (parseInt(col.substr(1)) == newRows[i]){
             
                  return  GRID[parseInt(col.substr(1)) - 1][convertColumn(col)];
             }
             else
             {
               return false;
             }
               
              
            }
  }

function isRock(col){

            if (lightCell(col) === '^')
            {
               return true;
               
            }else{
              
              return false;
          }
 }

function isCurrent(col){

            if (lightCell(col) === '~')
            {
               return true;
               
            }else{
              
              return false;
            }

}

function isShip(col){

            if (lightCell(col) === 'v')
            {
               return true;
               
            }else{
              
              return false;
            }

}

function lightRow(col){

 var newRows = [];
 
 for (var i = 0; i < GRID.length; i++){
              
               newRows[i + 1] = GRID[i];
 }
 return newRows[col];

}

function lightColumn(col){

    var  newColumn = [];
        
  
   for (var i = 0; i < GRID.length; i++){
     
       for (var j = 0; j < GRID[0].length; j++){
        
          newColumn[i] = GRID[i][convertColumn(col)];
       }
   }
   
   return newColumn;

}

function allRocks()
{

 var alpha = ['A','B','C','D','E','F','G','H','I','J','k','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
 
 var allrocks = [];
 
 
 for (var i = 0; i < countRows(); i++)
 {
   
    for (var j = 0; j < alpha.length; j++)
    {
      
       if (GRID[i][j] == '^')
       {
          allrocks.push(alpha[j]+(i+1));  
         
       }
       
    }
    
 }
 
 return allrocks;  

}

function allCurrents()
{

 var alpha = ['A','B','C','D','E','F','G','H','I','J','k','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
      
 var allcurrents = [];
 
 for (var i = 0; i < countRows(); i++)
 {
   
  
    for (var j = 0; j < alpha.length; j++)
    {
       if (GRID[i][j] == '~')
       {
        
        allcurrents.push(alpha[j]+(i+1));  

       }
       
    }

 }

 return allcurrents;
}

#19

For this challenge you want to go through each cell of our GRID, which you are doing, plus more. Our GRID is 10x10 as you know. With your two ‘for loops’ you are going through a GRID size of 10x26. The inside ‘for loop’ only needs to go to the count of columns, not the count of all possible letters. I believe if you change that for both allRocks and allCurrents, it will work. Fix that up and let us know if it works. If not I’ll take another look.

Edit - I see on another post you may already have this fixed up, take a look a my post below about your var alpha.


#20

It also looks like you have a lower case ‘k’ in your var alpha. Make that an uppercase ‘K’.