Do not trust the parsed value
Parsing JSON does not establish that the result is a number.
your task
Keep seatCount’s parameter and return types. Treat the parsed value as unknown. Return a number for numbers and text that converts to a number. For other parsed values, throw “Seats must be a number”.
The service can send the seat count as a JSON number or as text. The starter passes the type check but returns text for "4". It also accepts a boolean as a seat count.
Run code does two things. First the compiler checks the types and lists each error with its line. Then your code runs once for each case. A type error does not stop the run. The exercise is solved when there are no type errors and every case passes.
Starter code
or press Ctrl EnterNot run yet
type check
Not run yetcount.ts
Expected result: no type errors.
cases
Not run yetA number, including zero
Expected output
1
4 number2
0 numberSee the input and the code for this case
function show(text) {
try {
const seats = seatCount(text);
console.log(seats + " " + typeof seats);
} catch (error) {
console.log("error: " + error.message);
}
}
show("4");
show("0");Not run yetA number sent as text
Expected output
1
4 numberSee the input and the code for this case
function show(text) {
try {
const seats = seatCount(text);
console.log(seats + " " + typeof seats);
} catch (error) {
console.log("error: " + error.message);
}
}
show('"4"');Not run yetA boolean or nonnumeric text
Expected output
1
error: Seats must be a number2
error: Seats must be a numberSee the input and the code for this case
function show(text) {
try {
const seats = seatCount(text);
console.log(seats + " " + typeof seats);
} catch (error) {
console.log("error: " + error.message);
}
}
show("true");
show('"four"');