0% found this document useful (0 votes)
0 views

AP Computer Science Principles Program Code (4)

The document outlines a workout application that allows users to view, add, and manage their workouts and repetitions by day. It includes a list of exercises, a feature to shuffle and display a selection of exercises, and functionality to track the number of repetitions for each day of the week. Users can also add new workouts, remove workouts, and update their workout list and repetition counts through various interactive elements.

Uploaded by

aliaareel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

AP Computer Science Principles Program Code (4)

The document outlines a workout application that allows users to view, add, and manage their workouts and repetitions by day. It includes a list of exercises, a feature to shuffle and display a selection of exercises, and functionality to track the number of repetitions for each day of the week. Users can also add new workouts, remove workouts, and update their workout list and repetition counts through various interactive elements.

Uploaded by

aliaareel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

var workouts = [];

var repsByDay = {};


var repCount = 0;
var exercises = [
{name: "Push-ups"},
{name: "Squats"},
{name: "Plank"},
{name: "Lunges"},
{name: "Jumping Jacks"},
{name: "Bicep Curls"},
{name: "Tricep Dips"},
{name: "Burpees"},
{name: "Deadlifts"},
{name: "Bench Press"},
{name: "Pull-ups"},
{name: "Mountain Climbers"},
{name: "Leg Raises"},
{name: "Russian Twists"},
{name: "Shoulder Press"}
];

onEvent("showMenuButton", "click", function() {


var menu = "Try these exercises today:\n";
var shuffled = shuffleArray(exercises).slice(0, 5);
for (var i = 0; i < shuffled.length; i++) {
menu += (i+1) + ". " + shuffled[i].name + "\n";
}
setText("menuArea", menu);
});

function shuffleArray(array) {
var arr = array.slice();
for (var i = arr.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}

setProperty("dayDropdown", "options", ["Sunday", "Monday", "Tuesday", "Wednesday",


"Thursday", "Friday", "Saturday"]);
setText("dayDropdown", getDayName());
getKeyValue("myWorkouts", function(data) {
workouts = data || ["Push-ups", "Squats", "Plank", "Burpees"];
updateWorkoutList();
});

getKeyValue("repsByDay", function(data) {
repsByDay = data || {};
updateRepForSelectedDay();
});

onEvent("addWorkoutButton", "click", function() {


setScreen("addWorkoutScreen");
});

onEvent("submitWorkoutButton", "click", function() {


var newWorkout = getText("newWorkoutInput");
if (newWorkout !== "") {
workouts.push(newWorkout);
updateWorkoutList();
setText("newWorkoutInput", "");
setKeyValue("myWorkouts", workouts);
setScreen("mainScreen");
}
});

onEvent("backButton", "click", function() {


setScreen("mainScreen");
});

onEvent("workoutDropdown", "change", function() {


var selected = getText("workoutDropdown");
setText("selectedWorkoutLabel", "You selected: " + selected);
});

onEvent("dayDropdown", "change", function() {


updateRepForSelectedDay();
});

onEvent("repButton", "click", function() {


var day = getSelectedDay();
repsByDay[day] = (repsByDay[day] || 0) + 1;
setKeyValue("repsByDay", repsByDay);
updateRepForSelectedDay();
});

onEvent("removeRepButton", "click", function() {


var day = getSelectedDay();
if ((repsByDay[day] || 0) > 0) {
repsByDay[day]--;
setKeyValue("repsByDay", repsByDay);
updateRepForSelectedDay();
}
});

onEvent("removeWorkoutButton", "click", function() {


var selected = getText("workoutDropdown");
var index = workouts.indexOf(selected);
if (index !== -1) {
workouts.splice(index, 1);
updateWorkoutList();
setKeyValue("myWorkouts", workouts);
setText("selectedWorkoutLabel", "Workout removed.");
}
});

function updateWorkoutList() {
setProperty("workoutDropdown", "options", workouts);
}

function updateRepForSelectedDay() {
var day = getSelectedDay();
repCount = repsByDay[day] || 0;
setText("repCountLabel", "Reps (" + day + "): " + repCount);
}

function getSelectedDay() {
return getText("dayDropdown");
}

function getDayName() {
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return days[(new Date()).getDay()];
}

You might also like