Ticket Booking System
Programming Basics · JavaScript
TASK
Problem
Given free seats `free`, requested `need`, balance, and ticket `price`. Booking succeeds if there are enough seats and enough money. Print `BOOKED` and remaining balance, otherwise `REJECTED`.
EXAMPLE
Example
5 2 1000 300
BOOKED 400
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Ticket Booking System
`if / else if / else` chooses a branch from boolean conditions. Comparisons such as `<`, `>=`, `===` and logical operators `&&`, `||`, `!` combine the exact rules that decide which output is valid.
NEED HELP?
Hints
+
['Translate every case from the statement into a boolean condition before writing branches.', 'Do not mix the loop counter with the accumulated answer; those variables serve different roles.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [f, n, b, p] = input.trim().split(/\s+/).filter(Boolean).map(Number);
let cost = n * p;
if ((n <= f) && (cost <= b)) {
console.log("BOOKED", (b - cost));
} else {
console.log("REJECTED");
}