<< Back

Exercise 1.1 - Buttons and Inputs

d) Area and circumference

This is the console. All output created with writeLog() is shown here.

Please type the radius of a circle and press "Calculate". I will print the areas and circumference for the circle to the console.


script.js

This is the JavaScript code behind the above application.

/* global writeLog, getInput */
writeLog("Solution for exercise 1.1 d)");
function calculate() {
// Get the radius from the user
var radius = getInput("radius");
// Make it a number
radius = parseFloat(radius);
writeLog("You entered the following radius: " + radius);
var area = calculateArea(radius);
var circumference = calculateCircumference(radius);
writeLog("The area is: " + area.toFixed(2));
writeLog("The circumference is: " + circumference.toFixed(2));
}
// A function to calculate the area
function calculateArea(radius) {
return Math.PI * radius * radius;
}
// A function to calculate the circumference
function calculateCircumference(radius) {
return 2 * Math.PI * radius;
}