Workout Routine Generator JavaScript, API
👤 Sharing: AI
```javascript
// This code provides a basic framework. You'll need to:
// 1. Choose and integrate a suitable workout API (e.g., ExerciseDB, MuscleWiki API - many are free but may require registration and API keys).
// 2. Implement error handling and input validation.
// 3. Consider data persistence (local storage, databases) for user preferences.
// 4. Add more sophisticated logic for exercise selection (equipment, experience level).
// Placeholder API URL - REPLACE WITH AN ACTUAL API URL
const API_URL = "https://example.com/api/exercises"; // Replace this!!
async function fetchExercises(muscleGroup = null, type = null) {
let url = API_URL;
if (muscleGroup) {
url += `?muscle=${muscleGroup}`; // Example query parameter
}
if (type) {
url += `&type=${type}`; // Added type query param
}
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching exercises:", error);
return []; // Return an empty array or handle the error as needed
}
}
function generateWorkout(exercises, numExercises = 5) {
if (!exercises || exercises.length === 0) {
return "No exercises available. Please try different criteria.";
}
const workout = [];
const shuffledExercises = [...exercises].sort(() => Math.random() - 0.5); // Shuffle
for (let i = 0; i < Math.min(numExercises, shuffledExercises.length); i++) {
workout.push(shuffledExercises[i]);
}
if (workout.length === 0) {
return "No exercises matched the criteria.";
}
let workoutString = "Your Workout:\n";
workout.forEach((exercise, index) => {
workoutString += `${index + 1}. ${exercise.name} - ${exercise.type} (Target: ${exercise.muscle})\n`; // Adjust based on API response format
});
return workoutString;
}
// Example usage (you'll need to adapt this for user interaction)
async function main() {
const muscleGroup = prompt("Enter a muscle group (e.g., chest, back, legs) or leave blank for all:");
const exerciseType = prompt("Enter a type of exercise (e.g. cardio, strength) or leave blank for all:");
const exercises = await fetchExercises(muscleGroup, exerciseType);
const workout = generateWorkout(exercises, 5); //Generate 5 exercises
console.log(workout);
// Display the workout in a user-friendly way (e.g., in an HTML element).
//document.getElementById("workout-output").innerText = workout; // Example if you're using HTML
}
main();
// Example Data Structure (mimicking a typical API response)
// Adapt based on the actual API you use!
/*
[
{
"id": 1,
"name": "Bench Press",
"type": "strength",
"muscle": "chest",
"equipment": "barbell",
"difficulty": "intermediate",
"instructions": "Lie on a bench and press the barbell up."
},
{
"id": 2,
"name": "Squats",
"type": "strength",
"muscle": "legs",
"equipment": "barbell",
"difficulty": "intermediate",
"instructions": "Stand with the barbell across your upper back and squat down."
},
{
"id": 3,
"name": "Running",
"type": "cardio",
"muscle": "legs",
"equipment": "none",
"difficulty": "beginner",
"instructions": "Run at a moderate pace."
}
]
*/
```
👁️ Viewed: 12
Comments