-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotateButtons.html
74 lines (70 loc) · 2.68 KB
/
rotateButtons.html
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
<!--
Challenge:
Create nine buttons enclosed in a div, laid out so they form a 3x3 grid. Each button has a distinct label from 1 through 9, and the labels on the outer buttons must rotate in the clockwise direction each time the middle button (i.e., 5) is clicked.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Buttons Grid</title>
<style>
#btns {
margin: 0 auto;
width: 75%;
}
.btn {
font-size: 24px;
height: 48px;
width: 30%;
}
</style>
</head>
<body>
<div id="btns">
<button id="btn1" class="btn">1</button>
<button id="btn2" class="btn">2</button>
<button id="btn3" class="btn">3</button>
<button id="btn4" class="btn">4</button>
<button id="btn5" class="btn">5</button>
<button id="btn6" class="btn">6</button>
<button id="btn7" class="btn">7</button>
<button id="btn8" class="btn">8</button>
<button id="btn9" class="btn">9</button>
</div>
<script>
const rotateButtons = () => {
const buttons = document.getElementsByClassName('btn');
const mapping = {
1: 4,
2: 1,
3: 2,
4: 7,
6: 3,
9: 6,
8: 9,
7: 8
}
for (let i = 0; i < buttons.length; i++) {
let currentVal = parseInt(buttons[i].innerHTML);
if (currentVal !== 5) {
let newVal = mapping[currentVal];
buttons[i].innerHTML = newVal;
}
}
}
const rotateButtonsAlternative = () => {
let tmp = document.getElementById('btn1').innerHTML;
document.getElementById('btn1').innerHTML = document.getElementById('btn4').innerHTML;
document.getElementById('btn4').innerHTML = document.getElementById('btn7').innerHTML;
document.getElementById('btn7').innerHTML = document.getElementById('btn8').innerHTML;
document.getElementById('btn8').innerHTML = document.getElementById('btn9').innerHTML;
document.getElementById('btn9').innerHTML = document.getElementById('btn6').innerHTML;
document.getElementById('btn6').innerHTML = document.getElementById('btn3').innerHTML;
document.getElementById('btn3').innerHTML = document.getElementById('btn2').innerHTML;
document.getElementById('btn2').innerHTML = tmp;
}
document.getElementById('btn5').addEventListener('click', rotateButtons);
// document.getElementById('btn5').addEventListener('click', rotateButtonsAlternative);
</script>
</body>
</html>