HOW DO I USE THE METHOD ALERT HERE OR DOCUMENT.WRITE TO DISPLAY WHAT IT TURNED OUT?
INPUT DATA [['XXX', 3], ['XXXXX', 6], ['XXXXXX', 9], ['XXX',2]], 4
function meeting(x, need){
if (need == 0) { return 'Game On' }
var res = [], total = 0
for (let i = 0; i < x.length; i++) {
let chair = x[i][1] - x[i][0].length
if (Math.sign(chair) == -1) { chair = 0 }
if (chair <= need-total) { res.push(chair) }
else { res.push(need-total) }
total += chair
if (total >= need) { return res }
}
return 'Not enough!'
}
INFO
Your meeting room can take up to 8 chairs. need will tell you how many have been taken. You need to find that many.
Find the spare chairs from the array of meeting rooms. Each meeting room array will have the number of occupants as a string. Each occupant is represented by 'X'. The room array will also have an integer telling you how many chairs there are in the room.
You should return an array of integers that shows how many chairs you take from each room in order, up until you have the required amount.
example: [['XXX', 3], ['XXXXX', 6], ['XXXXXX', 9], ['XXX',2]] when you need 4 chairs:
result -- > [0, 1, 3] (no chairs free in room 0, take 1 from room 1, take 3 from room 2. No need to consider room 4 as you have your 4 chairs already.
If you need no chairs, return 'Game On'. If there aren't enough spare chairs available, return 'Not enough!'
Examples:
meeting([['XXX', 3], ['XXXXX', 6], ['XXXXXX', 9]], 4) ---> [0, 1, 3]
meeting([['XXX', 1], ['XXXXXX', 6], ['X', 2], ['XXXXXX', 8], ['X', 3], ['XXX', 1]], 5) ---> [0, 0, 1, 2, 2]
meeting([['XX', 2], ['XXXX', 6], ['XXXXX', 4]], 0) ---> 'Game On'
meeting([['XX', 2], ['XXXX', 6], ['XXXXX', 4]], 4) ---> 'Not enough!'
Aucun commentaire:
Enregistrer un commentaire