What can be a quick way to sort array in wave format but starting from specific number
for example: starting from point: 4 array: [10,6,0,8,4,2]
and every time output must be randomized, wave in different direction.
Advertisement
Answer
function rand(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } let raznos = [ 0.0061, 0.00611, 0.00609, 0.00612, 0.00608, 0.00613, 0.00607, 0.00614, 0.00606, 0.00615, 0.00605, 0.00616, 0.00604, 0.00603, 0.00602, 0.00601 ]; let start = 0.00610; raznos.sort((a, b) => Math.abs(a - start) - Math.abs(b - start) || b - a); if (rand(0,1)){ let new_raznos = []; new_raznos.push(raznos[0]); for(let i=2; i<=raznos.length; i=i+2){ if(raznos[i]){ new_raznos.push(raznos[i]); } new_raznos.push(raznos[i-1]); } raznos = new_raznos; } console.log(raznos);