0

I've been trying to resolve the following problem:

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 for each letter (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!).

Here is the code I wrote:

function walkTime(walk) {
    //insert brilliant code here
    var walkLength = walk.length;
    if (walkLength === 10) {
      return true;
    }
    else {
      return false;
    }
  }

findEndPosition = (Arr) => {
    var y = 0
    if (Arr.length > 10) {
        return false
    }
    else {    
    Arr.forEach((x) => {
        if (x === 'n') {
            y++;
        }
        if (x === 's') {
            y--;
        }
        if (x === 'e') {
            y++;
        }
        if (x === 'w') {
            y--;
        }
    })
    };
    if (y === 0) {
        return true;
    }
    else {
        return false;
    }
}

const isValidWalk = (walk) => {
    if(walkTime(walk) === true && findEndPosition(walk) === true){
        return true
    }
    else {
        return false
    }
}


console.log(isValidWalk(['w','e','w','e','w','e','w','e','w','e','w','e']));

I keep trying to passing all the tests except for two. Unfortunately it's not telling me what inputs it's using that keep failing :/. Anyone think they know what's going on, this is driving me crazy! Thanks in advance!

enter image description here

8
  • Without a minimal reproducible example of what the failing input is, it's not easy to debug, we can't guess it for you Commented Apr 23, 2021 at 4:12
  • grrr you are right, I was hoping since you guys have more experienced you might be able to spot it or take a guess at what's going on lol. Some of the people on here are wizards. I'm still on my first few weeks of learning to code Commented Apr 23, 2021 at 4:15
  • But we can help...if you describe it more Commented Apr 23, 2021 at 4:16
  • Given that I couldn't really see the input I'm not sure what else I could include that would make it a better experience for you? Commented Apr 23, 2021 at 4:16
  • 1
    It seems like going north, then going west returns you to the same position (you calculate y++, then y--). Whereas you're moving in two different dimensions. You need two variables to track this - x and y. One for N/S one for E/W. Then check if they are both zero. Commented Apr 23, 2021 at 5:48

2 Answers 2

1

If your goal is only to check if the walk will take exactly 10 minutes, it should be enough to test that the walk array is 10 in length and the walk steps are valid. Isn't it so?

In your code, you have a function for solving the end position. There is a clear mistake as steps to west and east are changing the y variable. Is your actual problem more related to this one?

Sign up to request clarification or add additional context in comments.

1 Comment

End goal: walk isn’t longer than 10 steps (each step = 1 min) AND starting position should be the same.
0

Just wanted to let you guys know I found this solution. It worked through all the tests! I'm sure it could be written a lot better, if you guys got suggestions I'd appreciate it! Anyways here it is!

function walkTime(walk) {
    //insert brilliant code here
    var walkLength = walk.length;
    if (walkLength === 10) {
      return true;
    }
    else {
      return false;
    }
  }

const findEndPosition = (Arr) => {
    var y = 0;
    var x = 0;
    if (Arr.length > 10) {
        return false
    }
    else {    
    Arr.forEach((movement) => {
        if (movement === 'n') {
            y++;
        }
        if (movement === 's') {
            y--;
        }
        if (movement === 'e') {
            x++;
        }
        if (movement === 'w') {
            x--;
        }
    })
    };
    const newLocation = [x,y];
    if (newLocation.toString() === '0,0') {
        return true;
    }
    else {
        return false;
    }
}

const isValidWalk = (walk) => {
    if(walkTime(walk) === true && findEndPosition(walk) === true){
        return true
    }
    else {
        return false
    }
}

console.log(isValidWalk(['n','s','e','w','n','s','e','w','e','w']));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.