Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page
  • a) Hello World!
  • b) Odd or even
  • c) Calculate the sum
  • d) Area and circumference
  • e) Repeating things
  • f) Validate input
  • g) Math and libraries
  • h) Change the header

Was this helpful?

  1. 1 - Getting Started
  2. Exercises

1.1 Buttons and Inputs

In this exercise, you implement some simple features using the app template. The goal is to become familiar with HTML and JavaScript as well as with the app template.

PreviousExercisesNextOverview

Last updated 3 years ago

Was this helpful?

Make a copy of the for each of the tasks below, and write the necessary code with JavaScript and HTML to solve the tasks below.

a) Hello World!

Add the necessary code to write the text "Hello World!" to the console!

Use the writeLog() function from the web helper library!

Solution:

b) Odd or even

Allow the user to enter a number. Write a program that determines whether that number is odd or even when the user presses a button. Print the result to the console!

Use the getInput() function from the web helper library!

Use an if-then-else statement to check whether the number is odd or even!

Solution:

c) Calculate the sum

Allow the user to enter two numbers. Write a program that adds both values together when the user presses a button. Print the result to the console!

Use parseFloat() to convert a input string into a number!

Solution:

d) Area and circumference

Allow the user to enter a number and interpret the number as the radius of a circle. Write a program that calculates the area and circumference of that circle and prints it to the console!

Create separate functions to calculate the area and circumference!

Solution:

e) Repeating things

Use a loop and choose the appropriate type for this scenario!

Solution:

f) Validate input

Use a control structure to decide whether an input is valid!

Solution:

g) Math and libraries

Add two input fields. When the user presses a button, take the value from the first input field to the power of the number in the second input field and print the result to the console.

Solution:

h) Change the header

This is a little useless, but shows an important point: when someone presses the button, change the heading to the text that was entered in the input field.

Solution:

The function will truncate a number at 2 decimal points.

Re-use your program from to automatically calculate the area and circumference of circles with a radius of 1, 2, 3, and up to 100. Print all results to the console.

Make sure that your program from can handle malicious input, such as negative values for a radius. Furthermore, clean up the input field after every press of the button.

You can get a reference to an input field via its unique id with . The field value contains the current value of an input field, which you can set to an empty string.

Check out the functionMath.pow()to perform the calculation. The contains numerous useful functions.

To get the heading in JavaScript, add an id to it and use

toFixed(2)
document.getElementById()
Math library
document.getElementById()
d)
d)
application template
Exercise 1.1 - Buttons and Inputs | a) Hello World!
Exercise 1.1 - Buttons and Inputs | b) Odd or even
Exercise 1.1 - Buttons and Inputs | c) Calculate the sum
Exercise 1.1 - Buttons and Inputs | d) Area and circumference
Exercise 1.1 - Buttons and Inputs | e) Repeating things
Exercise 1.1 - Buttons and Inputs | f) Validate input
Exercise 1.1 - Buttons and Inputs | g) Math and libraries
Exercise 1.1 - Buttons and Inputs | h) Change the header