isValidWalk
6kyu kata
by trentHarlem
JavaScript
const isValidWalk = (walk) => {
let x = 0, y = 0;
let start = [0, 0];
let location;
//let start
walk.forEach(step => {
location = [x, y];
switch (step) {
case 'n':
y += 1;
//console.log('x', x, 'y', y);
break;
case 's':
y -= 1;
//console.log('x', x, 'y', y);
break;
case 'w':
x += 1;
//console.log('x', x, 'y', y);
break;
case 'e':
x -= 1;
//console.log('x', x, 'y', y);
break;
}
return [x, y];
})
console.log('len',walk.length);
console.log( [x, y]);
if (walk.length!==10 || [x, y] !== [0, 0]) {
return false
} else {
return true;
}
}
//----------
/* const isValidWalk = (walk) => {
let x = 0,
y = 0;
let start = [0, 0];
let location;
//let start
walk.forEach(step => {
location = [x, y];
switch (step) {
case 'n':
y += 1;
console.log('x', x, 'y', y);
break;
case 's':
y -= 1;
console.log('x', x, 'y', y);
break;
case 'w':
x += 1;
console.log('x', x, 'y', y);
break;
case 'e':
x -= 1;
console.log('x', x, 'y', y);
break;
}
//return [x, y];
// console.log('start', start)
console.log('[x, y]: inside',[x, y])
console.log('start: inside',start)
console.log('location',location)
return [x, y];
})
//return x!==0 || y!==0?false:true;
//console.log('start', start)
console.log('[x, y]: final',[x, y])
console.log('x, y: final',x, y)
console.log('walk length',walk.length)
//return [x, y] === start ? true : false;
//return [x, y] !== start ? false : true;
if ((walk.length===10) && ([x, y] === [0, 0])) {
return true
} else {
return false;
}
if (walk.length!==10 || [x, y] !== [0, 0]) {
return false
} else {
return true;
}
if (walk.length!==10) {
return false
} else {
return false;
}
...