Skip to content

Instantly share code, notes, and snippets.

@selfup
Last active August 25, 2017 08:21
Show Gist options
  • Save selfup/01b8bfb6b4c7ee6a60d88f2ad610bb77 to your computer and use it in GitHub Desktop.
Save selfup/01b8bfb6b4c7ee6a60d88f2ad610bb77 to your computer and use it in GitHub Desktop.
// using this instead of scoped vars (regular function)
function saveWorld(largeBucket, smallBucket, result) {
this.largeBucketA = largeBucket;
this.smallBucketA = 0;
this.movesCounter = 0;
while (this.largeBucketA !== result && this.smallBucketA !== result) {
if (this.largeBucketA === largeBucket && this.smallBucketA === 0) {
this.largeBucketA = largeBucket - smallBucket; // 3
this.smallBucketA = smallBucket; // 4
this.movesCounter++;
} else if (smallBucketA === smallBucket) {
this.smallBucketA = 0;
this.movesCounter++;
} else if (largeBucketA === 0) {
this.largeBucketA = largeBucket
this.movesCounter++
} else {
if (smallBucket > this.largeBucketA) {
this.smallBucketA = this.largeBucketA
this.largeBucketA = 0
this.movesCounter++
} else {
this.largeBucketA = this.largeBucketA - (smallBucket - this.smallBucketA);
this.smallBucketA = smallBucket;
this.movesCounter++;
}
}
}
return this.movesCounter;
}
console.log(saveWorld(5, 3, 4));
//////////////////////////////////////////////////////
// creating a constructor and prototype methods
// a good exercise would be to re-write the object as a class
function SaveWorld(largeBucket, smallBucket, result) {
this.largeBucket = largeBucket;
this.smallBucket = smallBucket;
this.result = result;
this.largeBucketA = largeBucket;
this.smallBucketA = 0;
this.movesCounter = 0;
}
SaveWorld.prototype = {
work() {
this.checkConditions();
return this.movesCounter;
},
checkConditions() {
while (this.largeBucketA !== this.result && this.smallBucketA !== this.result) {
if (this.largeBucketA === this.largeBucket && this.smallBucketA === 0) {
this.firstCondition();
} else if (this.smallBucketA === this.smallBucket) {
this.secondCondition();
} else if (this.largeBucketA === 0) {
this.thirdCondition();
} else {
this.fourthCondition();
}
}
},
firstCondition() {
this.largeBucketA = this.largeBucket - this.smallBucket; // 3
this.smallBucketA = this.smallBucket; // 4
this.movesCounter++;
},
secondCondition() {
this.smallBucketA = 0;
this.movesCounter++;
},
thirdCondition() {
this.largeBucketA = this.largeBucket
this.movesCounter++
},
fourthCondition() {
if (this.smallBucket > this.largeBucketA) {
this.smallBucketA = this.largeBucketA
this.largeBucketA = 0
this.movesCounter++
} else {
this.largeBucketA = this.largeBucketA - (this.smallBucket - this.smallBucketA);
this.smallBucketA = this.smallBucket;
this.movesCounter++;
}
},
};
const sw = new SaveWorld(5, 3, 4);
const result = sw.work();
console.log(result);
////////////////////////////////////////////////////
// removing the need for let - and mimicking the first function without lexical this
const svwd = (largeBucket, smallBucket, result) => {
const state = {
largeBucketA: largeBucket,
smallBucketA: 0,
movesCounter: 0,
};
while (state.largeBucketA !== result && state.smallBucketA !== result) {
if (state.largeBucketA === largeBucket && state.smallBucketA === 0) {
state.largeBucketA = largeBucket - smallBucket; // 3
state.smallBucketA = smallBucket; // 4
state.movesCounter++;
} else if (state.smallBucketA === smallBucket) {
state.smallBucketA = 0;
state.movesCounter++;
} else if (state.largeBucketA === 0) {
state.largeBucketA = largeBucket
state.movesCounter++
} else {
if (smallBucket > state.largeBucketA) {
state.smallBucketA = state.largeBucketA
state.largeBucketA = 0
state.movesCounter++
} else {
state.largeBucketA = state.largeBucketA - (smallBucket - state.smallBucketA);
state.smallBucketA = smallBucket;
state.movesCounter++;
}
}
}
return state.movesCounter;
};
const svwdResult = svwd(5, 3, 4);
console.log(svwdResult);
/////////////////////////////////////////////////////
// using a closure to mimic a constructor but without the use of broken out prototypical methods
const svwd2 = (largeBucket, smallBucket, result) => {
const state = {
largeBucketA: largeBucket,
smallBucketA: 0,
movesCounter: 0,
};
return () => {
while (state.largeBucketA !== result && state.smallBucketA !== result) {
if (state.largeBucketA === largeBucket && state.smallBucketA === 0) {
state.largeBucketA = largeBucket - smallBucket; // 3
state.smallBucketA = smallBucket; // 4
state.movesCounter++;
} else if (state.smallBucketA === smallBucket) {
state.smallBucketA = 0;
state.movesCounter++;
} else if (state.largeBucketA === 0) {
state.largeBucketA = largeBucket
state.movesCounter++
} else {
if (smallBucket > state.largeBucketA) {
state.smallBucketA = state.largeBucketA
state.largeBucketA = 0
state.movesCounter++
} else {
state.largeBucketA = state.largeBucketA - (smallBucket - state.smallBucketA);
state.smallBucketA = smallBucket;
state.movesCounter++;
}
}
}
return state.movesCounter;
}
};
const setup = svwd2(5, 3, 4);
const setupResult = setup();
console.log(setupResult);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment