-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
75 lines (68 loc) · 2.15 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
let array = [1,2,3,4,5,6,7,8,9]; // array of input
let sortBtn = document.getElementById('sortBtn');
let suffleBtn = document.getElementById('suffleBtn');
/**
*
* @param {*} arr : takes in input of array and generates grid with style
*/
let cardGenerator = (arr) => {
let gridContainer = document.getElementById('g-container');
gridContainer.innerHTML = '';
arr.forEach((ele, i) =>{
let numChild = document.createElement('div');
numChild.id = 'num_'+i;
numChild.className = 'cell';
let clientWidth = document.documentElement.clientWidth;
let color = '';
switch(ele.toString()) {
case '1':
case '8': color = '#6F98A8';
break;
case '3':case '5':case '9': color = '#2F454E';
break;
case '2': case '4':color = '#2B8EAD';
break;
case '6': case '7': color = '#BFBFBF';
break;
}
if (clientWidth < 376) {
numChild.style.backgroundColor = '#EFEFEF';
numChild.style.borderLeft = '5px solid '+ color;
} else {
numChild.style.backgroundColor = color;
numChild.style.borderLeft = '0px';
}
numChild.innerHTML = arr[i];
gridContainer.appendChild(numChild);
});
}
window.onload = () => {
let titleMsg = 'Shuffle and Sort';
let creator = ' by Sachin Saurabh';
document.getElementById('header').innerHTML = titleMsg;
document.getElementById('footer').innerHTML = titleMsg + creator;
cardGenerator(array);
};
window.addEventListener('resize', () => {
cardGenerator(array);
});
sortBtn.addEventListener('click', () => {
array = array.sort();
cardGenerator(array);
});
suffleBtn.addEventListener('click', () => {
array = randomiser(array);
cardGenerator(array);
});
/**
*
* @param {*} arr on click of shuffle button takes array and shuffles the index of the array data.
*/
let randomiser = (arr) => {
const newArr = [];
for ( let i = arr.length-1; i>=0; i--) {
let index = Math.floor(Math.random()*(i));
[arr[i], arr[index]] = [arr[index], arr[i]];
}
return arr;
}