xxxxxxxxxx
70
let happiness;
function preload() {
result = loadStrings(womenshappiness.csv);
}
function setup() {
createCanvas(400, 400);
print("csvLine: " + csvLine);
// Get invidual parts splitting on the ',' characters
let words = split(csvLine, ",");
print(words);
// What would happen if we split on 'i'?
let others = split(csvLine, "i");
print(others);
print("grades in setup: " + grades);
print("Header row");
print(grades[0]);
// For calculating the overall grade
let totalPoints = 0;
let totalOutof = 0;
for (let i = 1; i < grades.length; i++) {
// Split the line on commas - we get an array
let values = split(grades[i], ",");
// We get the values *as strings* in the array
// we convert some values to float (number) so
// we can do math with them
let name = values[0];
let points = float(values[1]);
let outof = float(values[2]);
// Calculate the percentage
let percentage = points / outof * 100;
print("On assignment " + name + " you got " +
points + " out of " + outof + " (" + percentage + "%)");
// Add to the totals
totalPoints += points;
totalOutof += outof;
}
let finalPercentage = totalPoints / totalOutof * 100;
// Using toFixed(1)) to format the percentage as string
// with 1 digit after zero (e.g. 96.4)
print("You got " + finalPercentage.toFixed(1) + "% overall");
if (finalPercentage > 90) {
print("Congrats - you got an A!");
}
}
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
}