The function returns 'Above boundary' if obj.value is greater than 10, else 'Within boundary'.
Step 2: Check the input value
The input object has value 15, which is greater than 10, so it returns 'Above boundary'.
Final Answer:
"Above boundary" -> Option B
Quick Check:
Value 15 > 10 = "Above boundary" [OK]
Hint: Compare value to boundary condition carefully [OK]
Common Mistakes:
Confusing greater than with less than
Expecting undefined return
Thinking function throws error
4.
Find the error in this TypeScript code that tries to enforce a boundary on a number:
function setBoundary(value: number) { if (value < 0 || value > 100) { throw new Error('Value out of boundary'); } return value;}console.log(setBoundary(150));
medium
A. The error message is missing a semicolon
B. The boundary check should use && instead of ||
C. The function should return a string, not number
D. No error; code works as expected
Solution
Step 1: Analyze the boundary condition
The condition checks if value is less than 0 OR greater than 100, which correctly detects out-of-bound values.
Step 2: Check the function behavior
If value is out of boundary, it throws an error; otherwise, returns the value. Calling with 150 triggers the error as expected.
Final Answer:
No error; code works as expected -> Option D
Quick Check:
Boundary check with || is correct [OK]
Hint: Use || to check if value is outside boundaries [OK]
Common Mistakes:
Replacing || with && breaks boundary logic
Expecting return type mismatch error
Worrying about missing semicolon in error message
5.
You want to create a function that accepts an object with a number property score but only allows values between 0 and 100 inclusive. Which approach best enforces this boundary in TypeScript?
hard
A. Use a type guard function that checks score and throws an error if out of range
B. Use any type and trust the caller to pass correct values
C. Declare score as number without checks
D. Use a string type for score to avoid number boundaries
Solution
Step 1: Understand boundary enforcement
To enforce boundaries, code must check values and handle out-of-range cases explicitly.
Step 2: Evaluate options
Use a type guard function that checks score and throws an error if out of range uses a type guard function to check and throw errors, which actively enforces boundaries. Others either skip checks or use wrong types.
Final Answer:
Use a type guard function that checks score and throws an error if out of range -> Option A
Quick Check:
Active checks enforce boundaries best [OK]
Hint: Check values explicitly to enforce boundaries [OK]
Common Mistakes:
Trusting caller without checks
Using wrong data types to avoid boundaries
Ignoring boundary enforcement in code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.