Challenge 19 - Problem with Code


#1

Here’s what I have so far for Challenge 19. What am I missing?

function percentageReport(){
  var percentageRock = (allRocks.length/totalCells())*100;
  var percentRock = percentageRock.toFixed(2);
  var percentageCurrent = (allCurrents.length/totalCells())*100;
  var percentCurrent = percentageCurrent.toFixed(2);
  var report = [];
  report.push(percentRock);
  report.push(percentCurrent);
  return report;
}

Thanks!


#2

Try calling allRocks() and allCurrents() as they are functions, currently missing the ()


#3

I tried that, but I still get this error message “Your code ran but did not produce the correct result.” Any other ideas?


#4

Modified your code to include () and then tested it against my code:

console.log(percentageReport());
console.log(percentageReportEnumis());

The logs returned in order:

["9.00", "7.00"]
["9.00", "7.00"]

These leads me to believe the error lies outside of your percentageReport() function, either within the callRocks() or allCurrents() functions.

Hopefully this helps as a starting point!


#5

I am running into issues right now also, however I feel the issue stems from .toFixed(2) producing a String. This is why the output is [“9.00”,“7.00”] and not [9.00, 7.00]. I am unsure if the lighthouse checks will pick up that difference. I have attempted variations of parseFloat( […] ) however it converts it back to [9,7]. Definitely a tricky one.


#6

Additionally it is considered bad practice to conduct type conversions that way. Math.round( […] ) produces similar issues as the parseFloat.


#7

I utilized the following structure:

pReport[0] = parseFloat(allRocks().length/ totalCells() * 100).toFixed(2)

to store my returned value into my final array. Did the same for currents but targeted pReport[1] prior to returning pReport.

Not sure about bad practice/type conversions, as I am new to coding, but it worked for me.

@coding_journey Could you elaborate on your comment so that I could look into this further and extend my knowledge? Thanks!


#8

I apologize, I was late for an event earlier so my last message is more abrupt than intended. I am still learning myself, but while conducting my research for this topic I came across a lot of people criticizing the methodology. My understanding is that it is very process/memory intensive so on a larger scale it would be a major waste of resources. Also, I came across a method called .push for adding entries to the end of an array. That I think proves useful for this sort of situation. percentageValues.push(parseFloat(allRocks().length.toFixed(2)));


#9

Also, I am unsure if the parseFloat is impacting your formula. As you are converting (allRocks().length/ totalCells() * 100) with the parseFloat(x) then the .toFixed(2) is running. So the chain appears to go integer -> float -> string. Again I could be wrong, but I think that is how the chain of events runs. If you run a console.log your results will likely be [“9.00”,“7.00”] if it has the “” around each number then I think it is a string.


#10

It definitely produces a correct result once you add “()” to all of your functions like that.

function percentageReport(){
var percentageRock = (allRocks().length/totalCells())*100;
var percentRock = percentageRock.toFixed(2);
var percentageCurrent = (allCurrents().length/totalCells())*100;
var percentCurrent = percentageCurrent.toFixed(2);
var report = [];
report.push(percentRock);
report.push(percentCurrent);
return report;
}

Please copy your code to a REPL of some sort or browser and run it again to see the result or at least what errors it throws at you. And could you please post both your allRocks() and allCurrents() functions here?

If you still want to fix it yourself try debugging it like that

function percentageReport(){

var percentageRock = (allRocks.length/totalCells())*100;

console.log(allRocks());
console.log(allRocks().length);
console.log(totalCells());
console.log(percentageRock);

var percentRock = percentageRock.toFixed(2);
var percentageCurrent = (allCurrents.length/totalCells())*100;
var percentCurrent = percentageCurrent.toFixed(2);
var report = [];
report.push(percentRock);
report.push(percentCurrent);
return report;
}

good luck :slight_smile:


#11

I feel your pain man, I also wanted to return a float that would look like 7.00 and 9.00, but afaik it seems like it’s impossible. It always cuts unnecessary digits after the dot. If you find a way to do it, please share with us :smiley:


#12

Here’s the allRocks and allCurrents functions that I have:

function allRocks() {
var rocks = [];
GRID.map((row, rIndex) => {
row.map((column, cIndex) => {
if (column === “^”) {
var rockPlace = String.fromCharCode(cIndex + 65) + (rIndex + 1);
rocks.push(rockPlace);
}
return;
});
});
return rocks;
}

function allCurrents() {
var cell = [];
for (var ii = 0; ii < countRows(); ii++) {
for (var jj = 0; jj < countColumns(); jj++) {
var coordinatePoint = String.fromCharCode(jj + 65) + (ii + 1);
if (isCurrent(coordinatePoint)) {
cell.push(coordinatePoint);
}
}
}
return cell;
}


#13

You had a very small mistake. Just change

if (column === “^”)

to

if (column === “^”)

Good luck!

UPDATE:
Sorry this was probably due to this forum’s formatting of quotation marks. It even changed the quotation marks in my answer to different ones and it looked like I just copied the same stuff twice. So turns out it was not a problem. Other than that your code worked fine when I copied it and produced a correct result. Will look into it more.


#14

@coding_journey No need to apologize! Curious about the effects of parseFloat(x) and the toFixed(y) myself.

From what I saw here, parseFloat(x) helps to ensure that the returning argument comes back as a float. I was unsure what type of value .length() results in but I was worried that the calculation would come back as a rounded INT which wouldn’t be ideal for grids smaller or larger than 100 cells (not sure what test cases are being applied to our code when we submit).

Further, there is another thread (can’t seem to find it!) which is discussing the inability to return trailing zeros, something about them getting cut-off? Hence the need to then format into fixed-point notation ? Didn’t notice it returned a string until I was reading the discussion of others after I had submitted the code :blush:

It’s amazing how these small challenges result in so much reading, definitely learning more from this experience than I was expecting!


#15

Can you please send your totalCells() function too? That’s the only thing I can think of that could go wrong. Other than that everything should work once you add () to your functions.


#16

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


#17

I am sorry, but once I substitute your code for mine it still produces the correct result. Super weird, that it still doesn’t accept your answer. Anyway, good job on solving the challenge, I guess the only thing you can do now is just write directly to Lighthouse labs support.

Also try to check that you are not calling any functions before you declare them.

Since you have already solved the challenge, but it just doesn’t accept the answer try substituting your code for mine and see if it works.

let letterArr = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.split("");

function totalCells() {
return countColumns()*countRows();
}

function allRocks() {
let rocksArr = [];
for (let i = 0; i < GRID.length; i++) {
for (let b = 0; b < GRID[0].length; b++) {
if (GRID[i][b] == “^”) {
rocksArr.push(${letterArr[b]}${i+1});
}
}
}
return rocksArr;
}

function allCurrents() {
let currentsArr = [];
for (let i = 0; i < GRID.length; i++) {
for (let b = 0; b < GRID[0].length; b++) {
if (GRID[i][b] == “~”) {
currentsArr.push(${letterArr[b]}${i+1});
}
}
}
return currentsArr;
}

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