[Solved] Challenge 20 - I don't think I get it


#1

Hello,

so this is what I have until now.

function safetyReport() {

  let mapGrid = GRID.map(function(val) {
  return val;
  })
  console.log(mapGrid);
}

safetyReport();
console.log(safetyReport());

So essentially, I created a new array called mapGrid and whatever I return on the “function(val)” will be mapped to GRID which will then go into mapGrid.

So basically where I’m stock is that I’m not sure what to return.

Just the let mapGrid up until the first console.log, will log the whole array. So I have to find something that will replace the array values with 0, 50 and 100. I tried to just do val = howDagerous but obliviously it doesn’t work.

I’ve seen other people nesting other maps inside of the first map, but it get’s confusing.

What should I do? I have the howDangerous function that return the percent value when a coordinate is given, but in here I’m not passing any arguments…


#2
  1. val right now is your row in the GRID. You would want to iterate over val and check on each item.
  2. To use howDangerous() alongside with map, you need to have a implement someway so that you would have a GRID that tells you about cell locations instead.

Hope this helps.


#3
function safetyReport() {

  let mapGrid = GRID.map(function(row){
  GRID[row].map(function(column){
   SOME LOGIC HERE
  })
  })
  console.log(mapGrid);
}

safetyReport();
console.log(safetyReport());

Ok so the first one, accessed the rows. Now I know that GRID[row] accesses the columns??. I then map with a function that will change the values??


#4

row right now is an array so if you do GRID[row], I believe it won’t work. Remember that row is an array already at this point so you could do row.map() or a for-loop on row. Then, in the callback function for row.map() is where your logic should go.


#5

So the inside should look like this?

let mapGrid = GRID.map(function(row){
  row.map(function(column){
    LOGIC;
  })
  })
  console.log(mapGrid);

#6

That looks good to me. If you wanna be sure, before row.map() do console.log(row) so that you can see what value you are actually doing it. Then inside row.map() you can do console.log(column) so that you can see what the value of column is.

Also, I haven’t done callback functions in map with the ES5 row.map(function(row) {...}) notation so I am not entirely sure where you would have to put return.


#7

Ok so first, the console.log helps.

Now I copied somebody elses code and tweeked it a bit and added it to mine. Now I can get to return both the first row and the converted one, because the method I call. howDangerousValue already loops through it once?

let rock = "^";
let current = "~";

function howDangerousValue(value){
  if (value === rock){
    return 100;
  } else if (value === current){
    return 50;
  } else {
    return 0; 
  }
}

function safetyReport() {

  let mapGrid = GRID.map(function(row){
    console.log(row);
  row.map(function(column){
    console.log(column);
    howDangerousValue(column);
    console.log(howDangerousValue(column));
  })
    return mapGrid;
  });
  console.log(mapGrid);
}

safetyReport();
console.log(safetyReport());

To be honest I think this is where my knowledge of Javascript ends. I’m studying for another class, so i can’t focus on it now, but shit it pisses me off.


#8

let mapGrid = GRID.map(function(row){
row.map(function(column){
LOGIC;
})
})
console.log(mapGrid);

If you are using syntax with brackets you should also add “return”.

let mapGrid = GRID.map(function(row){
return row.map(function(column){
return LOGIC;
})
})
console.log(mapGrid);

Don’t forget there is a cleaner way, you could try syntax without brackets

const safetyReport = () = GRID.map(row => row.map(cell => LOGIC));

You can actually solve this challenge in one line of code. Just look at my last example and insert the correct logic. Don’t forget you can use ternary operators instead of if:) DO NOT GIVE UP, YOU GOT THIS!


#9

I’m going to be real i got stuck and copied somebody’s code and tried to do it my way, but when I console logged, it could create a long list instead of multiple arrays. And if I would call the function inside the two " }) ", it would throw an infinite loop and I would have to exit the page completely.

So I copied using the arrow functions and using the “return” and it worked. Now I’m too busy, but Ill come back later to try and figure out what was going on?

I think I need to really understand how does the map access the grid and what does the second map does exactly, etc.

Thanks