curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69306364df283fcaff2e1ad7.md
Given a 4x2 matrix (array of arrays) representing the seating arrangement for a meal, determine how many seats a left-handed person can sit at.
In the given matrix:
"R" is a seat occupied by a right-handed person."L" is a seat occupied by a left-handed person."U" is an unoccupied seat.For example, in the given matrix:
[
["U", "R", "U", "L"],
["U", "R", "R", "R"]
]
The top-left seat is cannot be sat in because there's a right-handed person to the left. The other two open seats can be sat in because there isn't a right-handed person to the left.
findLeftHandedSeats([["U", "R", "U", "L"], ["U", "R", "R", "R"]]) should return 2.
assert.equal(findLeftHandedSeats([["U", "R", "U", "L"], ["U", "R", "R", "R"]]), 2);
findLeftHandedSeats([["U", "U", "U", "U"], ["U", "U", "U", "U"]]) should return 8.
assert.equal(findLeftHandedSeats([["U", "U", "U", "U"], ["U", "U", "U", "U"]]), 8);
findLeftHandedSeats([["U", "R", "U", "R"], ["L", "R", "R", "U"]]) should return 0.
assert.equal(findLeftHandedSeats([["U", "R", "U", "R"], ["L", "R", "R", "U"]]), 0);
findLeftHandedSeats([["L", "U", "R", "R"], ["L", "U", "R", "R"]]) should return 1.
assert.equal(findLeftHandedSeats([["L", "U", "R", "R"], ["L", "U", "R", "R"]]), 1);
findLeftHandedSeats([["U", "R", "U", "U"], ["U", "U", "L", "U"]]) should return 5.
assert.equal(findLeftHandedSeats([["U", "R", "U", "U"], ["U", "U", "L", "U"]]), 5);
function findLeftHandedSeats(table) {
return table;
}
function findLeftHandedSeats(table) {
let availableSeats = 0;
const [topRow, bottomRow] = table;
for (let i=0; i<topRow.length; i++) {
if (topRow[i] === 'U') {
if (i === 3) {
availableSeats++;
} else if (topRow[i+1] !== 'R') {
availableSeats++;
}
}
}
for (let i=0; i<bottomRow.length; i++) {
if (bottomRow[i] === 'U') {
if (i === 0) {
availableSeats++;
} else if (bottomRow[i-1] !== 'R') {
availableSeats++;
}
}
}
return availableSeats;
}