-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
add task solution #548
Conversation
There was a problem hiding this 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.
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
DEMO LINK