Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsity Eval #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Parsity Eval #239

wants to merge 1 commit into from

Conversation

Backuphero
Copy link

had a lot of fun and also a little bit of trouble with this Eval. My mentor was a huge help with pushing me over the hump! had a little extra fun at the end trying to create a cheat code "cheatCode()" executable in the chrome console.

@@ -0,0 +1,110 @@
// Initial game board
var towersBoard = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont use var, either const or let

console.log('Invalid move, large discs may not be place on smaller ones. Try again');
};
};
function checkWinner() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check your spacing

Suggested change
function checkWinner() {
function checkWinner() {


Array.prototype.myForEach = function(callback) {
for (let i = 0; i < this.length; i++) {
var element = this[i];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using var, just made this available to the whole scope

]
};

Array.prototype.myForEach = function(callback) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a recreation of the already existing forEach helper function?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not recommended for larger applications. Using a normal function is better and more maintainable.

function customForEach(arr, callback) {
  for (let i = 0; i < arr.length; i++) {
    callback(arr[i]);
  }
}

Comment on lines +23 to +41
function moveDisc(fromPeg, toPeg) {
if (towersBoard.board[fromPeg].length === 0) {
console.log('No disc to move');
return;
};

var disc = towersBoard.board[fromPeg][towersBoard.board[fromPeg].length - 1];
var destinationPeg = towersBoard.board[toPeg];

// Make sure that the move is valid (smaller disc on top of larger disc)
if (destinationPeg.length === 0 || disc < destinationPeg[destinationPeg.length - 1]) {
towersBoard.board[toPeg].push(towersBoard.board[fromPeg].pop());
console.log(`Moved disc ${disc} from Peg ${fromPeg + 1} to Peg ${toPeg + 1}`);
displayBoard();
checkWinner();
} else {
console.log('Invalid move, large discs may not be place on smaller ones. Try again');
};
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your code alignment makes the code not really readable. Hard to understand where the function ends.

Comment on lines +26 to +27
return;
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this, just do an else and call a function.

};

function moveDisc(fromPeg, toPeg) {
if (towersBoard.board[fromPeg].length === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to help with readability this check could have been a const

Suggested change
if (towersBoard.board[fromPeg].length === 0) {
const isPegEmpty = towersBoard.board[fromPeg].length === 0;
if (isPegEmpty) {

var destinationPeg = towersBoard.board[toPeg];

// Make sure that the move is valid (smaller disc on top of larger disc)
if (destinationPeg.length === 0 || disc < destinationPeg[destinationPeg.length - 1]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are missing validation for moving a disc from a peg X to the same X, that is an invalid move

Comment on lines +17 to +21
function displayBoard() {
towersBoard.board.myForEach(function(peg) {
console.log(peg);
});
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function displayBoard() {
towersBoard.board.myForEach(function(peg) {
console.log(peg);
});
};
function displayBoard() {
towersBoard.board.forEach((peg) => console.log(peg));
}

};
};
function checkWinner() {
if (towersBoard.board[1, 2].length === 5) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not really scalable nor dynamic, if we wanted to add a new peg, will need a lot of refactor.
function isWinningTower(tower) {
return tower.length === 5 && tower.every((disc, index) => disc === 5 - index);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants