articles/smalltalk.md: add factorial example
This commit is contained in:
parent
ec1e691191
commit
b899e7e291
1 changed files with 11 additions and 0 deletions
|
@ -61,6 +61,17 @@ String extend [
|
||||||
Transcript show: 'smalltalk' removeVowels; cr. "==> smlltlk"
|
Transcript show: 'smalltalk' removeVowels; cr. "==> smlltlk"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Factorial
|
||||||
|
Smalltalk already provides a method `factorial` but that's cheating so we will implement it here:
|
||||||
|
```smalltalk
|
||||||
|
| n res |
|
||||||
|
res := 1.
|
||||||
|
n := 5.
|
||||||
|
1 to: n do: [:i | res := res * i].
|
||||||
|
Transcript show: 'fact of ', (n asString), ' = ', (res asString); cr.
|
||||||
|
```
|
||||||
|
Homework: add this as a method by extending the `Number` class.
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
|
|
||||||
## Resources
|
## Resources
|
||||||
|
|
Loading…
Add table
Reference in a new issue