MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / isValidWalk

Function isValidWalk

arrays/isValidWalk.js:1–17  ·  view source on GitHub ↗
(walk)

Source from the content-addressed store, hash-verified

1function isValidWalk(walk) {
2 let n = 0;
3 let s = 0;
4 let e = 0;
5 let w = 0;
6
7 if(walk.length < 2 || !walk.length) return false;
8 for(const direction of walk) {
9 if(direction === 'n') n++;
10 if(direction === 's') s++;
11 if(direction === 'e') e++;
12 if(direction === 'w') w++;
13 }
14
15 if(n + s + e + w > 10) return false;
16 return ((n + s + e + w < 10) ? false : n - s === 0 && e - w === 0);
17}
18
19 const walk =['n','n','n','s','n','s','n','s','n','s']
20console.log(isValidWalk(walk));

Callers 1

isValidWalk.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected