Challenge 19: Code not working


#1

Not sure what I’m missing. Can anyone help? I don’t know if the answer should be written as a string [“9.00”, “7.00”] or a numbers [9,7]. I can’t seem to write the array with two decimal points as my console just ignores the zeros after the integer.

    const GRID = [
          ["", "", "", "^", "", "", "", "", "", ""],
          ["", "", "v", "", "~", "", "", "", "", ""],
          ["", "v", "", "", "^", "^", "", "", "", ""],
          ["", "", "", "", "^", "^", "", "", "", ""],
          ["", "", "", "", "", "", "", "", "v", ""],
          ["", "", "", "", "", "", "", "", "", ""],
          ["", "", "", "", "", "", "", "", "", ""],
          ["", "^", "~", "~", "", "", "", "^", "", ""],
          ["", "^", "", "~", "~", "", "", "", "", ""],
          ["", "^", "", "", "~", "~", "", "", "", ""],
        ];
      
    function totalCells(){
      return GRID.length * GRID[0].length;
    }

    //totalCells();
    //print => 100


    function indexToChar(i) {
      return String.fromCharCode(i+97).toUpperCase(); 
    }

    function allRocks(){
      rockcoordinates=[];
      for(var i = 0; i < GRID.length; i++){
        for(var j = 0; j < GRID[0].length; j++){
          if(GRID[i][j] == "^"){
            rockcoordinates.push(indexToChar(j)+ (i+1));
          }
        }
      }
      return rockcoordinates;
    }

    //allRocks();
    //print an array of all cells coordinates with "^" =>
    // (9) ["D1", "E3", "F3", "E4", "F4", "B8", "H8", "B9", "B10"]

    function allCurrents(){
      strongCurrents=[];
      for(var i = 0; i < GRID.length; i++){
        for(var j = 0; j < GRID[0].length; j++){
          if(GRID[i][j] == "~"){
            strongCurrents.push(indexToChar(j)+ (i+1));
          }
        }
      }
      return strongCurrents;
    }

    //allCurrents();
    //print an array of all cells coordinates with "~" =>
    // (7) ["E2", "C8", "D8", "D9", "E9", "E10", "F10"]

Possible answers that do not work:

    function percentageReport(){
       let numRocks = (allRocks().length / totalCells()) * 100;
       let numCurrents = (allCurrents().length / totalCells()) * 100;
       return [Math.round(numRocks * 100) / 100, Math.round(numCurrents * 100) / 100]
     }

     //(2) [9, 7];

     function percentageReport(){
       let numRocks = (allRocks().length / totalCells());
       let numCurrents = (allCurrents().length / totalCells());
       return [parseFloat(numRocks * 100).toFixed(2), parseFloat(numCurrents * 100).toFixed(2)];
     }

    // //(2) ["9.00", "7.00"]

     function percentageReport(){
       let totalRocks = (allRocks().length / totalCells()) * 100;
       let totalCurrents = (allCurrents().length / totalCells()) * 100;
       return [totalRocks.toFixed(2), totalCurrents.toFixed(2)];
     }

    // returns =>(2) ["9.00", "7.00"]    

     function percentageReport(){
       let totalRocks = (allRocks().length / totalCells()) * 100;
       let totalCurrents = (allCurrents().length / totalCells()) * 100;
       return [parseFloat(totalRocks.toPrecision(3)), parseFloat(totalCurrents.toPrecision(3))];
     }
     //(2) [9, 7]

     function percentageReport(){
       let totalRocks = (allRocks().length/totalCells()*100).toFixed(2);
       let totalCurrents = (allCurrents().length/totalCells()*100).toFixed(2);
       return [(totalRocks), (totalCurrents)];
     }

    // ["9.00", "7.00"]

    //percentageReport();

#2

You need to parseFloat the allRocks.length and totalCells during the calculations (to accommodate different GRID sizes and get a float value), then in the return statement parse back to a string with toFixed(2) for displaying of the answer.


#3

Thanks! I solved it! I think what I was also doing was copy & pasting the const GRID = [...] in my solution. So when I run the program, the computer recognizes two const GRID = [...] as an error. Once I deleted my const GRID, my solution worked. :grin:


#4

I struggled with the same “declaring two const GRID” problem for a whole day and nearly pulled all my hair out. :sweat_smile: