Hello, I’m working with a JS problem that seemed pretty basic to me on the surface, but I’m having trouble with it and would like some advice about how to proceed.
So I’ll give you the set-up and what I’ve written so far as well as where I think I’m wrong. Please clear up any misconceptions or incorrect logic. Thank you!
You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones – everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. [‘n’, ‘s’, ‘w’, ‘e’]). You always walk only a single block in a direction and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don’t want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.
> Note : you will always receive a valid array containing a random assortment of direction letters (‘n’, ‘s’, ‘e’, or ‘w’ only). It will never give you an empty array (that’s not a walk, that’s standing still!).
So, my basic idea was to loop through the way forward[i] and the way back[j] and assign those the same numerical value and if both were true, then the function would return true.
I think the problem might be in two sections:
1.Perhaps I am using the variable i and j incorrectly. Perhaps I should be using them together in some way.
2.Perhaps I should not be using length with i and j (I’m not sure exactly why I think this, but it seems like the only weak spot to me right now.
Thank you for your time.
(Obviously I’m at work on a CodeWars project. Much appreciation for any feedback you could provide.)
There are a few things that I’m not understanding about your solution so far
What is the significance of 10000? If walk is " random assortment of direction letters (‘n’, ‘s’, ‘e’, or ‘w’ only)" wouldn’t the length of every element be 1?
What is the significance of two elements having matching length?
I used 10000 because the challenge indicated that the walk must take exactly 10 minutes. However, upon reevaluation I can see that 1 makes more sense. My confusion now is in how to proceed from the following coding… function isValidWalk(walk) {
for(var i = 0;walk[i].length === 1;i++){
for(var j = 0;walk[j].length === 1;j++){
if(walk[i].length && walk[j].length === 10) {
return true;
} else {
return false;
}
}
}
My reasoning behind the if statement was to return true only if the departure (walk[i]) and return trip (walk[j]) would take only ten minutes total. I wonder if the flaw in my logic is the use of variables i and j. If this is not true, could you point where my logical flaw might be?
The essence is to check and make sure that sum of ver (stands for vertical) and hor (stands for horizontal) equals to 0 and check if the total steps (count or walkCount.length) equals 10 in my solution below.
function isValidWalk(walk) {
var ver = 0;
var hor = 0;
var walkCount = walk.map(direction => {
if (direction == 'n') {
return ver += 1
} else if (direction == 's') {
return ver -= 1
} else if (direction == 'w') {
return hor += 1
} else if (direction == 'e') {
return hor -= 1
}
})
var count = walkCount.length
return ver === 0 && hor === 0 && count === 10
}
isValidWalk(['n','s','n','s','n','s','n','s','n','s']), 'should return true')
This explains it better:
*
An alternate approach is to represent the directions into 2 columns, vertical and horizontal. Then the directions on the same axis are represented 1 and -1 . This way, knowing if you’re back is a matter of checking if both axes sum up to zero. No subtraction required.
*V H*
*N 1 0*
*S -1 0 *
*E 0 1*
*E 0 1*
*W 0 -1*
*Result: [0, 1] *
*// Off by 1 to the east. Also, length is less than 10.*
(P.S: I saw this explanation of Data transformations on Stackexchange read more here>>)