Challenge 17, issues


#22

The splice method will only work for allShips, here’s is the solution
function shipReport() {
const c = allShips().sort();
let a = [c[0], c[c.length - 1]];
return a;
}


#23

Here’s is the simple solution… sort the allShips method and return the array of the first and last element


#24

This is my solution. It’s pretty simple.

So in my function, I **sorted** all the ships and stored them in 
a variable.

I then created another variable and stored in it the first item of 
the sorted list.

I did the same thing for the last item.

I then created an empty array and **pushed** both variables 
in this new array.

Then I called the function which returned an array with both 
ships.

Hope this helps.


#25

Seems like they have made a mistake or haven’t clarified concisely what they consider West and East in this case. Tbh, I did the challenge when I just woke up and read through it so quickly that all I noticed was:

They want something similar to what you did for firstRock() and firstCurrent() but they want both the first AND the last ship in your area.

So I just returned the first and last element of allShips() like that:

function shipReport() {
let arr = [];
arr.push(allShips()[0], allShips().slice(-1)[0]);
return arr;
}

and it worked. I was so confused when I read through the challenge again and realized that it actually asked for something very different.


#26

maybe try to look into localeCompare() as another way of comparing strings


#28

I’m using the compare callback of the array.sort method to compare the letters of the coordinates. It works, but the only part I’m not satisfied with is if there were multiple ships in the A column for example, should it return all A ships or just the first one.


#30

I actually misread this question and just returned the first and last items in allShips(). Since it passed, I thought that was what the answer they were looking for even though they asked it really badly. It is pretty confusing to ask for the “first and last” and then “west (left) and east (right)” most ships. Those are probably going to be different positions for most GRIDs, including the example one.


#31

Here are my thoughts

My original thought after reading the challenge is "Okay, this should be really simple, all I have to do is return the first and last…I’ve already got all the functionality in firstShip(), allShips(), this will be easy. Upon dissecting those functions, I realized, like many, that the first ship is not “B3”.

Taking a closer look at how I wrote those functions (not using the .filter() method), I’m looping over all the rows (i.e. sub-arrays), then all the columns (of each sub-array). So, looking at the actual GRID variable, I see the first ship is at B3 and my expectation is that B3 should be the first coordinate that gets pushed into my returned array (I’m referring to allShips()).

Giving it some more thought, it becomes obvious why this is the case. Because allRows() is filtering through the rows first, then the columns, it becomes clear why C2 is pushed into the returned array, then B3 (i.e. we’re looping through all the columns - a, b, c, …, etc. - of row 2, then, all the columns of row 3).

How I interpreted the West-most and East-most is West-Most being the first-most letters (i.e. A, B, C, …), and East-Most as the latter-most letters (…, h, i, j).

I hope this gives some more clarification on what is what.

See here for the looping function


#32

Here’s my code solution:

function shipReport() {
  var allShipCoordinates = allShips();
  var returnArray = [];
  
  var westMostCoord = allShipCoordinates[0];
  var eastMostCoord = allShipCoordinates[0];
  
  allShipCoordinates.forEach(function(shipCoord, index){
    if(convertColumn(shipCoord)<convertColumn(westMostCoord)){
      westMostCoord = shipCoord;
    }
    
    if(convertColumn(shipCoord)>convertColumn(eastMostCoord)){
      eastMostCoord = shipCoord;
    }
    
  });
  
  returnArray.push(westMostCoord);
  returnArray.push(eastMostCoord);
  
  return returnArray;
}

When I run it in the console, I get [“B5”, “I5”] which seems to be the correct answer, but I get the response:
Your code ran but did not produce the correct result.

Super confused as to why.


#33

What does your convertColumn function look like? Mine was only checking out to Column “J” since that is how big the grid is, but I remember seeing somewhere that they wanted it to work for other size grids up to a maximum of “Z”. My challenge 17 code is very similar to yours and it worked fine in Code Pen but did not pass. Then I modified my convertColumn to look all the way out to “Z” and then it passed.


#34

Thanks for pointing that out! This is what my convertColumn code looks like:

function convertColumn(coordinateVal) {
  var begCharCode = 'A'.charCodeAt(0); //to find the relative position of 'A' in ASCII table.
  return coordinateVal.charCodeAt(0) - begCharCode; // '- begCharCode' so that position relative to 'A' is found.
}

Essentially maps the alphabet to index. Seems to work fine.

About the output, am I not supposed to get [“B5”, “I5”]?


#35

I am testing the function result on the console and its returning correctly: [“B3”, “I5”] but is showing that didnt produce the correct result… :confused:


#36

KISS method; Keep It Super Simple.

East and West on this grid is only concerned with the letters and we can forget about the numbers for this example.

That said when we run allShips() we are given ["B3", "C2", "I5"]
Now lets ignore the numbers because we’re not being asked about rows and only focus on the letters.
So our response, in this example, looks like ["C","B","I"]

What built in method do arrays have when dealing with organizing the values in an array?
Here is a hint, I’ll let you .sort() it out.

What happens if we slightly modify allShips()?
It is already returning an array, and so what do we get it we add .sort() to the end?
Well we’d get ["B","C","I"].

Now we need the furthest west and east which would be “B” and “I”.

How do we return only the first and last values from an array?
We know the first item in an array is in position 0, and the last value in an array is the array.length-1.
So then what is needed to solve this?

First we need to sort the array returned from allShips()
Then we need to extract only the first and last values.
Lastly return an array with these values.

// Original allShips() without sort()
const allShips = () => findAll('v'); // returns  ["C2", "B3", "I5"]

//Modified allShips() with sort()
const allShips = () => findAll('v').sort(); // returns  ["B3", "C2", "I5"]
const shipReport = () => [allShips()[0],allShips()[allShips().length-1]]; // returns ["B3", "I5"]

It is likely a better method to call allShips() only once instead of 3x but I’ve been making an effort to make all my answers be one line responses.

A more verbose response that doesn’t modify the original allShips() function, and doesn’t use the arrow function, would be:

function shipReport() {
    let ships = allShips().sort()
    return [ships[0], ships[ships.length-1]] }

#37

Doesn’t work for me unfortunately! Any idea why?


#38

What is your allShips() function returning?


#39

Did you copy and paste the answer?
If so I noticed a missing } at the end of the function (I’ve since edited the answer and added it)


#40

I tried that too and doesnt work.
I tried:
function shipReport() {
let allShipsArray = allShips().sort();
let extremesShips = [];
extremesShips.push(allShipsArray.shift(), allShipsArray.pop());

return extremesShips;
}
and
function shipReport() {
let allShipsArray = allShips().sort();
return [allShipsArray[0], allShipsArray[allShipsArray.length-1]];
}

they return proper result but still is not accepted.


#41

Can you create a codepen.io pen with your code including any functions needed to complete the process.

It is difficult to say why something isn’t working without seeing all the code.

I’m going to update my code to use .shift() and .pop(); good call!


Challenge #17...help!
#42

My allShips() function returns:

["C2", "B3", "I5"]


#43

Yeah I fixed that before running it, so that’s not the reason why this is not working. Is there someone from lighthouse that can fix this?