O'Reilly logo

The Official BBC Micro:bit User Guide by Gareth Halfacree

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix B

JavaScript Recipes

THIS APPENDIX CONTAINS complete code listings for the programs detailed in Chapter 6, ‘JavaScript’, and Chapter 8, ‘The Wireless BBC micro:bit’, presented without comments to make them easier to type in and compare against. When a line of code would extend past the border of the page, a ↩ symbol is printed. When you see this symbol, continue to type the code without pressing the Enter or Return keys. If you’re not sure how a line of code should be entered, visit the website at www.wiley.com/go/bbcmicrobituserguide to download plain-text versions of each program; these can then be used for reference or even simply copy and pasted directly into the editors.

Chapter 6: Hello, World! (Non-looping)

basic.forever(() => {})basic.showString("Hello, World!")

Chapter 6: Hello, World! (Looping)

basic.forever(() => { basic.showString("Hello, World!")})

Chapter 6: Button Inputs (Single Button)

input.onButtonPressed(Button.A, () => { basic.showIcon(IconNames.Happy)})

Chapter 6: Button Inputs (Two Buttons)

input.onButtonPressed(Button.A, () => { basic.showIcon(IconNames.Happy)})input.onButtonPressed(Button.B, () => { basic.showIcon(IconNames.Sad)})

Chapter 6: Touch Inputs

let touches = 0input.onPinPressed(TouchPin.P0, () => { touches += 1 basic.showNumber(touches)})

Chapter 6: Temperature Sensor (No Formatting)

basic.forever(() => { basic.showNumber(input.temperature())})

Chapter 6: Temperature Sensor (with Formatting)

basic.forever(() => { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required