Currently, I am in the process of developing a function that extracts chest exercises from an array titled "chest".
This particular function is required to randomly select multiple exercises, achieved by utilizing a random pointer. In order to avoid selecting duplicate exercises, I compare the chosen chest exercise (i.e., chest[pointer]) with all the values within the final array.
If the newly selected exercise is not already present in the final array, it is returned and subsequently added to the final array. However, if the exercise is already part of the final array, a recursive call to the function takes place. The objective here is for the function to repeatedly run recursively until it identifies a new exercise that has yet to be chosen:
Obtain Chest:
function getChest(arr){
var pointer = 0;
//random array pointer
pointer = Math.round(Math.random() * (chest.length - 1));
//check for duplicate
for(var i = 0; i < arr.length - 1; i++){
if(arr[i].name === chest[pointer].name){
return getChest(arr);
} else {
return chest[pointer];
}
}
};
The main function utilizes this approach to randomly pick exercises. The resulting array is identified as 'day':
function chooseExercises(){
for(i = 0; i <= 5; i++){
ex = getChest(day);
day.push(ex);
}
}
Despite my efforts, I am encountering duplicates when running the solution. Any insights into what may be causing this issue? (Note: I am implementing angularJS)