Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

really need help with this js interview question

there is a 25*25 matrix, it can generate any number of blocks at any position. The starting point and the end point is known, how to get the minimum length of path? (you can only move horizontally and vertically)

I really appreciate any help!

geoffrey
geoffrey
28,736 Points

I can't help you, I don't know the answer, however due to the question, I 'm curious, this interview is for which position ?

1 Answer

So you have a 25 X 25 list of coordinates, so coordinates will go something like: [0,0] [0,1] [0,2] ... [1,0] [1,1] [1,2] ... [2,0] [2,1] [2,2] ...

Both the start and end values should be given as (x,y) So you would first need to get from your x coord in start to your x coord in end, then get from your y coord in start to your y coord in end.

Lets say you are given:

var start = 12, 20; var end = 30, 3;

You would do something like this: var xStep = start[0] - end[0] var yStep = start[1] - end[1]

This can also tell you the direction if you consider that a positive xStep will mean that you need to travel left from your starting point and a positive yStep will tell you that you need to travel up from your starting point.

If you are only trying to find out what the distance would be then you would need something like this:

var steps = Math.abs(start[0] - end[0]) + Math.abs(start[1] - end[1]);

This will give you total steps no matter what direction.