JSFiddle - React, Tailwind, and code Playground
by CommandLineDesign
JavaScript
'use strict';
//Define Objects
const TripCalculator = function(){
this.position = {
x:0,
y:0
}
this.move = function(direction, distance){
return this.position[direction] += distance;
}
let thisObject = this;
this.directionMap = {
n:function(){return thisObject.move('y', 1)},
s:function(){return thisObject.move('y', -1)},
e:function(){return thisObject.move('x', 1)},
w:function(){return thisObject.move('x', -1)}
}
}
TripCalculator.prototype.walk = function(direction){
return this.directionMap[direction]();
}
//Validate Trip
const TripValidator = function(limit, goal, tripArray){
this.valid = true;
this.limit = limit;
this.goal = goal;
this.tripArray = tripArray;
this.trip = new TripCalculator();
}
TripValidator.prototype.checkTripLimit = function(){
if(tripArray.length > limit){
return this.valid = false;
}
return this.valid = true;
}
TripValidator.prototype.checkTripGoal = function(){
for(let i = 0; i < this.tripArray.length; i++){
return this.trip.walk(this.tripArray[i]);
}
}
TripValidator.prototype.validateTrip = function(){
this.checkTripLimit();
this.checkTripGoal();
return this.valid;
}
//Execute
const trip1 = new TripValidator(10, 0, ['n','s','e','w']);
console.log(trip1.validateTrip());