Referenced from lesson Functions That Ask Questions

Buggy Code Correct Code / Explanation

1

fun piecewisefun(n):
 if (n > 0): n
 else: 0

2

fun cost(topping):
 if string-equal(topping,
"pepperoni"): 10.50
 else string-equal(topping,
"cheese"): 9.00
 else string-equal(topping,
"chicken"): 11.25
 else string-equal(topping,
"broccoli"): 10.25
 else: "That's not on the menu!"
 end
end

3

fun absolute-value(a b):
 if a > b: a - b
 b - a
 end
end

4

fun best-function(f):
 if string-equal(f, “blue”):
 “you win!”
 else if string-equal(f, “blue”):
 “you lose!”
 else if string-equal(f, “red”):
 “Try again!”
 else: “Invalid entry!”
 end
end

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). CCbadge Bootstrap:Reactive by Emma Youndtsmith, Emmanuel Schanzer, Kathi Fisler, Shriram Krishnamurthi, Joe Politz and Dorai Sitaram is licensed under a Creative Commons 4.0 Unported License. Based on a work at www.BootstrapWorld.org. Permissions beyond the scope of this license may be available by contacting schanzer@BootstrapWorld.org.