Open
Description
openedon Mar 14, 2018
Copy past the following program to typescript
function gameOfLife() {
for (let b = 0; b <= 4; b++) {
let count = 0
basic.showNumber(count + b)
count += 1
}
}
gameOfLife()
Output: 0,1,2,3,4
Convert to blocks and back to typescript.
let count = 0
function gameOfLife() {
for (let b = 0; b <= 4; b++) {
basic.showNumber(count + b)
count += 1
}
}
gameOfLife()
Now the count
variable is hoisted and output is different (0,2,4,6,8). This is due to fact that set to count is not reset.
count
is treated as global variable in typescript. But we shouldn't get rid of set at the local scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment