Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Take A Ten Minute Walk - How to Access Array Elements Properly

If you're not familiar with this challenge, here are the instructions:

https://www.codewars.com/kata/54da539698b8a2ad76000228/train/javascript

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.

So far, I have tried:

function isValidWalk(walk) {

  //initiate person starting point
  let person = [0, 0]
  //establish what the ending point must be
  let finalDestination = [0, 0]

  let north = [0, 1]
  let east = [1, 0]
  let south = [0, -1]
  let west = [-1, 0]

  //as long as the length of the array is 10 or less, continue walk
  for (let i = 0; i <= 10; i++) {
    //if the letter in the array is "n", move north
    if (walk[i] === "n") {
      person + north;
    }
    //if the letter in the array is "e", move east
    if (walk[i] === "e") {
      person + east;
    }
    //if the letter in the array is "s", move south
    if (walk[i] === "s") {
      person + south;
    }
    //if the letter in the array is "w", move west
    if (walk[i] === "w") {
      person + west;
    }
  }

  if (person === finalDestination) {
    return true;
  }
  else {
    return false;
  }

}

And this is passing 6/9 tests, but it is not returning true for a valid walk.

As you can see, I tried to say that the isValidWalk function should return true if the location of person is equal to the finalDestination variable at the end of their walk.

I know that there are probably other ways to solve this problem, but I would like to continue following the logic that I've established if that's possible.

I'm wondering if my problem is that I'm not accessing the elements in the array correctly? I.e. is walk[i] getting the array elements correctly here?

    if (walk[i] === "n") {
      person + north;
    }

That is what should be moving the person around on this imaginary grid but apparently it's not doing anything. What other syntax should I try to access the elements in the array and check if they're equal to "n", "e", "s", and "w"?

like image 211
HappyHands31 Avatar asked Feb 07 '26 01:02

HappyHands31


1 Answers

First, JavaScript arrays don't work like mathematical matrices. array1 + array2 won't add the individual values in those arrays together. Instead, you'll have to increment/decrement the values inside your arrays:

if (walk[i] === "n") {
  person[0]++;
}
if (walk[i] === "e") {
  person[1]++;
}
if (walk[i] === "s") {
  person[0]--;
}
if (walk[i] === "w") {
  person[1]--;
}

Or more succinctly:

switch(walk[i]) {
  case "n": person[0]++; break;
  case "e": person[1]++; break;
  case "s": person[0]--; break;
  case "w": person[1]--; break;
}

Second, person and finalDestination are arrays, so === means reference equality. That is, person === finalDestination will only return true if both variables refer to the same location in memory. Instead, you need to compare the individual values of the arrays, e.g.

if (person[0] === finalDestination[0] &&
    person[1] === finalDestination[1]) {
  return true;
}
else {
  return false;
}

Or more succinctly:

return person[0] === finalDestination[0] &&
       person[1] === finalDestination[1];

However, note that finalDestination never changes, so you really don't even need that variable at all. You could just replace it with:

return person[0] === 0 && person[1] === 0;

One last point about the requirements:

return true if the walk the app gives you will take you exactly ten minutes

You'll want to add this to the top of your function:

if (walk.length !== 10) return false;

And for cleaner code, make sure your for-loop doesn't run past the end of walk by replacing i <= 10 with i < 10 or i < walk.length.

like image 164
p.s.w.g Avatar answered Feb 09 '26 16:02

p.s.w.g



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!