Challenge 21 help


#1

Hey,
I have been trying to start on todays challenge but i have no clue where to start.
I know that i’m needing to finish with finding the distance using:

calc = math.sqrt(aa + bb)

but i’m having a hard time taking a coordinate of A7, and turning it placing it in 2 arrays, so i can do a1 + a2 and b1 + b2.

If anyone could give me either some pointers or some help that would be great.


#2

You already have convertColumn(), do you have a convertRow()? If so use these functions to break the input arguements down into four local variables(x1,x2,y1,y2) and do the math from there. Could bypass arrays altogether!


#3

I used my convertRow() (I know not everyone made this function but it’s relatively simple) and convertColumn().

So coordA can be converted to aX and aY, and coordB can become bX and bY.

I also needed both Math.sqrt and Math.pow.


#4

Hi,
so i have convertColumn(), i don’t have convertRow however. How would i go about breaking up the variables,
cause if they input E7 it is together?
I have been trying to use GRID[parseInt(coordinate2[1])-1][convertColumn(coordinate2)]; from one of my other day codes, but i can’t seem to get anything to return to see it.
Sorry brand new to coding.


#5

You could use something like the following to take the “row” aspect from the coordinate:

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

This then accepts rows greater than 9 and reduces by 1 to accommodate for the zero index nature of the array.

You would then use your convertColumn() in tandem with the convertRow() to isolate the needed parts of the coordinate into local variables within your final function.

As an example, without giving to much away:

function calcDistance(coordinate1, coordinate2){
var xDist = Math.abs(convertColumn(coordinate2) - convertColumn(coordinate1));
...
}

would provide the “x” values that we have come to love as columns. You could then use a similar structure with the convertRow() function to find “y” which comes from our rows. Once you have these two “cartesian-orientated” distances you would then perform the remaining math. Does this help get you on the right track?


#6

Thank you very much for getting back to me.
I got it working this was my finishing code.

function calcDistance(coordinate, coordinate1){
first = [coordinate,coordinate1].toString();
x1 = first.toUpperCase().charCodeAt(0)-65;
x2 = first.toUpperCase().charCodeAt(3)-65;
y1 = first.charAt(1);
y2 = first.charAt(4);
a1 = y2-y1;
a2 = x2-x1;
calc = Math.sqrt(a1a1 + a2a2).toFixed(2);
// second = GRID[parseInt(coordinate2[1])-1][convertColumn(coordinate2)];
//calc = math.sqrt();
console.log(x1,x2,y1,y2,a1,a2);
console.log(calc);
//console.log(first);
return calc;
}
calcDistance(“D5”,“B4”);
console.log(calc);

It is probably a really long winded way of getting it but it worked :slight_smile:
You had gotten me to starting thinking on the right track.


#7

cant identify what is wrong. the message is, Your code ran but did not produce the correct result.

function calcDistance(coord1, coord2){
return Math.sqrt(Math.pow((convertColumn(coord2)-convertColumn(coord1)),2)+(Math.pow(convertRow(coord2)-convertRow(coord1)),2)).toFixed(2);
}


#8

cant identify what is wrong. the message is, Your code ran but did not produce the correct result.

Here’s hopefully a helpful hint…When I ran calcDistance(‘B3’, ‘I5’) with my accepted final answer code, the result was 7.28. When I ran it with your code, the result was 7.14.


#9

thanks a ton.
now it is successfull.