@@ -22,27 +22,38 @@ class InputHandler {
2222 }
2323
2424
25- /**
26- *
27- * @param min
28- * @param max
29- * @returns {number }
30- */
31- static randomIntFromInterval = ( min , max ) => {
32- // https://gist.github.com/spyesx/485e4584aae767201f41
33- return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
34- } ;
35-
36-
3725 /**
3826 *
3927 * @returns {number }
4028 */
4129 static getAllowedMaxInputSize ( ) {
30+ const MAX_BAR_HEIGHT = window . innerHeight - CONSTANTS . OFFSET ;
4231 const MARGIN = CONSTANTS . margin ;
4332 const BAR_MIN_WIDTH = 1 ;
4433 const SCREEN_WIDTH = window . innerWidth ;
45- return Math . floor ( SCREEN_WIDTH / ( BAR_MIN_WIDTH + MARGIN ) ) ;
34+ const potentialNum = Math . floor ( SCREEN_WIDTH / ( BAR_MIN_WIDTH + MARGIN ) ) ;
35+
36+ return potentialNum <= MAX_BAR_HEIGHT ? potentialNum : MAX_BAR_HEIGHT ;
37+ }
38+
39+
40+ // https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
41+ static shuffle ( array ) {
42+ let currentIndex = array . length , randomIndex ;
43+
44+ // While there remain elements to shuffle.
45+ while ( currentIndex != 0 ) {
46+
47+ // Pick a remaining element.
48+ randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ;
49+ currentIndex -- ;
50+
51+ // And swap it with the current element.
52+ [ array [ currentIndex ] , array [ randomIndex ] ] = [
53+ array [ randomIndex ] , array [ currentIndex ] ] ;
54+ }
55+
56+ return array ;
4657 }
4758
4859
@@ -51,23 +62,10 @@ class InputHandler {
5162 * @param inputSize
5263 */
5364 static generateRandomIntArray ( inputSize ) {
54- let arr = [ ] ;
55- const MAX_BAR_HEIGHT = window . innerHeight - 280 ;
56-
57- for ( var i = 0 ; i < inputSize ; i ++ ) {
58- let duplicate = false ;
59- while ( ! duplicate ) { // SG 07/10/2022 22:04 not allowing duplicates
60- let potentialN = InputHandler . randomIntFromInterval ( 5 , MAX_BAR_HEIGHT ) ;
61- for ( const num of arr ) {
62- if ( potentialN === num ) duplicate = true ;
63- }
64- if ( duplicate ) duplicate = false ;
65- else {
66- arr . push ( potentialN ) ;
67- break ;
68- }
69- }
70- }
65+ const MAX_BAR_HEIGHT = window . innerHeight - CONSTANTS . OFFSET ;
66+ let arr = Array . from ( new Array ( MAX_BAR_HEIGHT ) , ( x , i ) => i + 5 ) ;
67+ arr = InputHandler . shuffle ( arr ) ;
68+ arr = arr . slice ( 0 , inputSize ) ;
7169 InputHandler . setInputArray = arr ;
7270 console . log ( InputHandler . getGeneratedArray )
7371 Strategy . setTargetArray = arr ;
0 commit comments