He hecho un sitio web donde puedes generar un entrenamiento aleatorio, es decir, un entrenamiento con 5 ejercicios aleatorios combinados con 5 tipos aleatorios de repeticiones. Para generar una cantidad aleatoria de repeticiones, usé Math.floor (Math.random() para la matriz que hice. Para generar 5 entrenamientos aleatorios diferentes, usé la función de reproducción aleatoria en Javascript para mezclar mi matriz cada vez que se recarga la página.
Ahora quiero que el usuario pueda guardar cualquier resultado que obtuvo en su página web en el almacenamiento local de su computadora para que pueda acceder a ese entrenamiento aleatorio específico cuando lo desee. como hago con esto???
Aquí abajo publico el código que creé para generar el resultado.
// This makes the reps generate randomly in a list of 5 let maxNr = 10; function generateRep(){ let randomReps = [`4x10`,`4x8`, `4x20`, `4x12`, `4x15`,`3x10`, `3x15`, `4x5`, `5x10`, `10x10`]; for(let i=0; i < 5; i++){ let randomNr = Math.floor(Math.random() * maxNr); if (randomNr==9) maxNr=9; let repsText = "<li>"+randomReps[randomNr]+"</li>"; document.getElementById("repsList").innerHTML+=repsText; console.log(maxNr); } } //THIS IS A SHUFFLE FUNCTION function shuffle(array) { let currentIndex = array.length, randomIndex; // While there remain elements to shuffle... while (currentIndex != 0) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; // And swap it with the current element. [array[currentIndex], array[randomIndex]] = [ array[randomIndex], array[currentIndex]]; } return array; } //This is the workout generator for, in this case, a chest and back workout using the shuffle function from above. function generateWorkout() { let workoutList = [`Chins`, `Wide barbell row (bent over)`, `Row with machine`, `Cable pulldown`, `Lat pulldown`, `Bent-over dumbbell alternating row`,`Reverse fly with barbell`,`Push-ups`, `Face-pull with cable`, `Seated face pull`, `Single arm lat pulldown`, `Low position row with cable`, `Split stance high anchor row with cable`, `Bench Press`, `Overhead press with dumbbells or barbell`, ` One arm row with dumbbell`,` Inverted row`, `Close grip dumbbell press`, ]; let shuffleWorkoutList= shuffle(workoutList); for(let i=0; i < 5; i++){ let workoutText = "<li>"+workoutList[i]+"</li>"; document.getElementById("listOfWorkouts").innerHTML+=workoutText; } } ```La sintaxis de usar LocalStorage es
localStorage.setItem("name-or-key", "value") donde la clave y el valor deben ser cadenas.localStorage.getItem("name-or-key")Así es como podría implementar esto en su fragmento:
function generateWorkout() { let workoutList = [`Chins`, `Wide barbell row (bent over)`, `Row with machine`, `Cable pulldown`, `Lat pulldown`, `Bent-over dumbbell alternating row`,`Reverse fly with barbell`,`Push-ups`, `Face-pull with cable`, `Seated face pull`, `Single arm lat pulldown`, `Low position row with cable`, `Split stance high anchor row with cable`, `Bench Press`, `Overhead press with dumbbells or barbell`, ` One arm row with dumbbell`,` Inverted row`, `Close grip dumbbell press`, ]; let shuffleWorkoutList= shuffle(workoutList); for(let i=0; i < 5; i++){ let workoutText = "<li>"+workoutList[i]+"</li>"; }; localStorage.setItem("workout-list", workoutList.join(",")); };y
function load_prev_workout() { const prev_workout = localStorage.getItem("workout-list").split(","); console.log(prev_workout); for(let i=0; i < 5; i++){ let workoutText = "<li>"+prev_workout[i]+"</li>"; console.log("Now use this:",workoutText,"for something."); }; };Luego llame a estos condicionalmente:
if(localStorage.getItem("workout-list")) { load_prev_workout(); } else { generateWorkout(); };Si desea guardar más de un entrenamiento a la vez, deje que el usuario elija un nombre para ese entrenamiento, o déle una identificación única, luego utilícelo como parte del nombre/clave del elemento localStorage