Aabha · Learn by building

Exercise 02: Convert before calculating

Use a copy of examples/02. Write answers before opening the solution.

A. Trace

With capacity 18 and interested 6, add one interested visitor, then calculate remaining. What are interested and remaining after each assignment? Would remaining change automatically if interested changed again?

B. Repair

A program calculates const total = "4" + 2. Make total the number 6 without changing the incoming text literal. Inspect typeof before and after conversion.

C. Transfer

Write a program that takes the text " 5 ", trims it, converts it to a number and reports whether it is finite. Repeat with "many" and an empty string. Explain why a blank check must come before conversion.

Evidence

Your trace and source. Success: interested 7, remaining 11; numeric 6; finite for trimmed 5, not finite for NaN, and recognition that empty text converts to 0.

Return to lesson · Explained answer