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(() => { ...

Get The Official BBC Micro:bit User Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.