Split the lane’s concrete bill with Mila: functions, procedures, then JavaScript and Python. 14 levels, played right in the browser with nothing to install.
What the levels ask
The residents of Kemangi Lane agreed to cast their lane in concrete before the rains, sharing the cost by the width of each house’s front. The neighbourhood head always works it out in his book: add up the lane’s length, split the cost per metre, then multiply by a house’s front. Mila has given each step a name. Put the three steps in order so Mila learns her own house’s share, house 2.
The treasurer wants to know how much money will come in before the slips are printed. But Mila’s share works out a share and prints its slip at the same time, so every time the treasurer only wants to count, slips come out too. Choose what goes inside share and how each slip is printed, so the output is exactly the neighbourhood head’s book: the total first, then one slip per house.
This morning Mila measures the lane in one walk, from its mouth to its end. She would like to print the slips as she walks too, to save time. Choose what is printed while she walks and what is printed once she reaches the end, so each house pays its own share.
Mila writes the inside of share herself. The main program keeps what share gives in amount, prints the slip, then adds it to collected. Choose what goes inside share so its number reaches amount, not just the screen.
The neighbourhood head writes the main program with names of his own: fee for the cost, metres for the lane’s length. The function perMetre(cost, laneLength) stays as Mila wrote it. Choose how to call it so the price per metre is right.
The materials are paid to Kokoh Mulyo Hardware, but the builders are paid in cash on the day the lane is cast, so the neighbourhood head splits them separately. The program calls share twice for each house: once with the materials price, once with the builders’ price. Choose what goes inside share so both columns are right.
Last year’s price, 140000 a metre, is still written on the post’s board, and the main program keeps it in price. The function perMetre has a price inside it too. Choose the line that puts this year’s price on the slips.
Mila adds two steps: cementSacks works out how many sacks of cement to buy (3 sacks for every 2 metres), and printHeader prints the heading above the slips. Look at how each step is called in the main program, then choose for each one: a FUNCTION that hands back a value, or a PROCEDURE that does something.
The slips will be printed from the old computer at the neighbourhood post, and it runs JavaScript in its browser. The neighbourhood head’s book (shares.pc) is already right. Complete slips.js so the computer prints exactly what the book says: the word that opens a function, the inside of share, and the comparison in the if.
Mila takes the slips from door to door, and her phone runs Python. Complete slips.py so the phone prints exactly the neighbourhood head’s book: the word that opens a function, the inside of share with its indentation, the line that adds up, and the comparison in the if.
In the neighbourhood head’s book, FOR i ← 0 TO LENGTH(fronts) - 1 runs i from 0 to 2, and 2 is included. JavaScript and Python write the end of a loop another way. Choose the loop line in slips.js and slips.py so both print exactly what the book says: three slips, then what is left of the money.
Cement went up. The cost is now 3655000, and 3655000 over 24 metres does not divide evenly. In the book, DIV (whole-number division) drops the decimals, and what is left goes to the neighbourhood’s cash fund. Choose the inside of perMetre in JavaScript and in Python so both print exactly what the book says.