<< Back

Exercise 1.1 - Buttons and Inputs

e) Repeating things

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

Press the button "Calculate" and I will give you the area and radius for 100 circles!


script.js

This is the JavaScript code behind the above application.

/* global writeLog, getInput */
writeLog("Solution for exercise 1.1 e)");
function calculate() {
// A for-loop to repeat the same code n times
for (var i = 1; i <= 100; i++) {
var area = calculateArea(i);
var circumference = calculateCircumference(i);
writeLog("Radius: " + i + ", area: " + area.toFixed(2) + ", circumference: " + 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;
}