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

add task solution #548

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

Conversation

KolomietsN
Copy link

Copy link

@VitaliyBondarenko1982 VitaliyBondarenko1982 left a comment

Choose a reason for hiding this comment

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

Good job.
In comment i wanted to show example how we can refactor and reduce amount of code.

Comment on lines +199 to +217
function moveLeft() {
for (let i = 0; i < numRows; i++) {
const leftShift = matrix[i];
const filteredRow = move(leftShift);

matrix[i] = filteredRow;
updateBoard();
}
}

function moveRight() {
for (let i = 0; i < numRows; i++) {
const rightShift = [...matrix[i]].reverse();
const filteredRow = move(rightShift);

matrix[i] = filteredRow.reverse();
updateBoard();
}
}

Choose a reason for hiding this comment

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

Suggested change
function moveLeft() {
for (let i = 0; i < numRows; i++) {
const leftShift = matrix[i];
const filteredRow = move(leftShift);
matrix[i] = filteredRow;
updateBoard();
}
}
function moveRight() {
for (let i = 0; i < numRows; i++) {
const rightShift = [...matrix[i]].reverse();
const filteredRow = move(rightShift);
matrix[i] = filteredRow.reverse();
updateBoard();
}
}
function moveHorizontally(direction = 'left') {
const isLeft = direction === 'left';
for (let i = 0; i < numRows; i++) {
const shift = isLeft ? matrix[i] : [...matrix[i]].reverse();
const filteredRow = move(shift);
matrix[i] = isLeft ? filteredRow : filteredRow.reverse();
updateBoard();
}
}

this two function has almost same logic. we can combine them in one function something like that

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.

3 participants