Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List myAverage := method(
- total := 0
- if (size == 0) then (
- nil
- ) else (
- foreach(x,
- if (x type == "Number") then (
- total = total + x
- ) else (
- Exception raise("Cannot get the average of non-numbers!")
- )
- total / size
- )
- )
- )
- list(1, 2, 3, 4) myAverage println // outputs 2.5
- list() myAverage println // outputs nil
- list(2, 3, 4) myAverage println // outputs 3
- list(2, "three", 4) myAverage println // throws an exception
Add Comment
Please, Sign In to add comment