From 1f582e8112896c0d1f5c50946f2d462a76e28051 Mon Sep 17 00:00:00 2001 From: Nicolas Bornand Date: Sat, 10 May 2014 18:57:48 +0200 Subject: add array clone in computePlaylist() --- app/js/services.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/js/services.js b/app/js/services.js index 15b1e09..142254d 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -11,8 +11,9 @@ app.service('pathService', function() { }, computePlaylist: function(songList, constrainList, duration) { //compute distances from the constrain path + var songListCopy = songList.slice(0); var maxDistOverall = Number.MIN_VALUE; - angular.forEach(songList, function(song){ + angular.forEach(songListCopy, function(song){ song.minDist = Number.MAX_VALUE; for(var i=0; i 0 && songList.length > 0){ + while(duration > 0 && songListCopy.length > 0){ //randomly select one song - var index = Math.floor(Math.random()*songList.length); - var song = songList[index]; + var index = Math.floor(Math.random()*songListCopy.length); + var song = songListCopy[index]; var tuneParameter = 2; var ratio = song.minDist/(maxDistOverall*tuneParameter); console.log(ratio); //favor the ones closer to the path var weight = 1 - ratio*ratio; if(Math.random() < weight){ - songList.splice(index, 1); + songListCopy.splice(index, 1); selection.push(song); } duration -= 10; -- cgit v1.2.3