I have a function that gets a random number between a min and max.
I call the function on a variable as such:
let randomPage = getRandomIntInclusive(1, 5);
That expression is repeated several times in my code (let's say 5 to 10 times within different functions). Is there a way to refactor it further or call it in a better way?
The way I call it is like:
function a() {
randomPage = getRandomIntInclusive(1, 5);
}
function b() {
randomPage = getRandomIntInclusive(1, 5);
}
function c() {
randomPage = getRandomIntInclusive(1, 5);
}
Full code:
$http({
method: "GET",
url: "URL"
})
.then(function mySuccess(response) {
$scope.discoverMovie = response.data;
/* console.log($scope.discoverMovie);
console.log($scope.discoverMovie.results[0].id); */
var i = getRandomIntInclusive(1, $scope.discoverMovie.results.length - 1);
trailerKey = $scope.discoverMovie.results[i].id;
getMovieDetail(trailerKey);
$scope.nextMovie = function () {
/* Get Movie to Next Movie Function */
i = i + 1;
if (i == $scope.discoverMovie.results.length) {
randomPage = getRandomIntInclusive(1, 5);
i = getRandomIntInclusive(1, $scope.discoverMovie.results.length - 1);
getMovieList(randomPage);
}
trailerKey = $scope.discoverMovie.results[i].id;
getMovieDetail(trailerKey);
};
$scope.previousMovie = function () {
/* Get Movie to Next Movie Function */
i = i - 1;
if (i < 0) {
randomPage = getRandomIntInclusive(1, 5);
i = getRandomIntInclusive(0, $scope.discoverMovie.results.length - 1);
getMovieList(randomPage);
}
trailerKey = $scope.discoverMovie.results[i].id;
getMovieDetail(trailerKey);
};
}, function myError(response) {
$scope.discoverMovie = response.statusText;
});