Hackerrank | 10 Days of Javascript | Day 8-Solution in JS | Buttons-Container-Hackerrank-Solution-in-JS


Hackerrank | 10 Days of Javascript | Day 8-Solution in JS | Buttons-Container-Hackerrank-Solution-in-JS

Hackerrank Solution 10-Days-of-JavaScript

Objective

In this challenge, we lay out buttons inside a div and modify their labels after each click event on one of the buttons. Check out the attached tutorial for learning materials.

Task
We want to create nine buttons enclosed in a div, laid out so they form a  grid. Each button has a distinct label from  to , and the labels on the outer buttons must rotate in the clockwise direction each time we click the middle button.

Complete the code in the editor so that it satisfies the following criteria:

  • Initial State. The initial layout looks like this:
    layout
  • Element IDs. Each element in the document must have an id, specified below:

    • The button container div's id must be btns.
    • The initial innerHTML labels must have the following button ids:
    innerHTMLid
    1btn1
    2btn2
    3btn3
    4btn4
    5btn5
    6btn6
    7btn7
    8btn8
    9btn9
  • Styling. The document's elements must have the following styles:
    • The width of btns is , relative to the document body's width.
    • Each button (i.e., btn1 through btn9) satisfies the following:
      • The width is , relative to its container width.
      • The height is 48px.
      • The font-size is 24px.
  • Behavior. Each time btn5 is clicked, the innerHTML text on the grid's outer buttons (i.e., bt1btn2btn3btn4btn6btn7btn8btn9) must rotate in the clockwise direction. Do not update the button id's.

The .js and .css files are in different directories, so use the link tag to provide the CSS file path and the script tag to provide the JS file path:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/buttonsGrid.css" type="text/css">
    </head>
    
    <body>
    	<script src="js/buttonsGrid.js" type="text/javascript"></script>
    </body>
</html>

Explanation

Initially, the buttons look like this:

initial

After clicking btn5  time, they look like this:click1

After clicking btn5  more time (for a total of  clicks), they look like this:click2


https://www.hackerrank.com/challenges/js10-create-a-button-container/problem
Day 8: Button Container Solution in JS
ButtonGrid.js
.btns { width: 75%; position: relative; } .btn { width: 30%; height: 48px; font-size: 24px; }
buttonGrid.js
class Rotator { constructor(values) { this.values = values; } rotation() { this.values.unshift(this.values.pop()); } round() { this.rotation(); this.render(); } render() { const [btn1, btn2, btn3, btn6, btn9, btn8, btn7, btn4] = this.values; const hashTable = {btn1, btn2, btn3, btn6, btn9, btn8, btn7, btn4}; for (const key in hashTable) { document.getElementById(key).innerHTML = hashTable[key]; } } } document.addEventListener('DOMContentLoaded', () => { const rotator = new Rotator([1, 2, 3, 6, 9, 8, 7, 4]); rotator.render(); document.getElementById('btn5').addEventListener('click', () => { rotator.round(); }); });
index.html
<pre><div id="" style="border: 2px; height: 400px; overflow: scroll;"> <!--Enter your HTML code here--> <html> <head> <meta charset="utf-8"></meta> <title>Buttons Grid</title> <div class="btns"> <div> <button class="btn" id="btn1">1</button> <button class="btn" id="btn2">2</button> <button class="btn" id="btn3">3</button> </div> <div> <button class="btn" id="btn4">4</button> <button class="btn" id="btn5">5</button> <button class="btn" id="btn6">6</button> </div> <div> <button class="btn" id="btn7">7</button> <button class="btn" id="btn8">8</button> <button class="btn" id="btn9">9</button> </div> </div> </head> <body> </body> </html>

Post a Comment

0 Comments