Algorithm


Hacker rank Problem Name: 10 Days of JavaScript - Day 8: Create a button

Hacker rank Problem Link: https://www.hackerrank.com/challenges/js10-create-a-button?isFullScreen=true&hr_b=1

Day 8: Create a Button

 

Objective

In this challenge, we practice creating buttons in JavaScript. Check out the attached tutorial for learning materials.

 

Task

Complete the code in the editor so that it creates a clickable button satisfying the following properties:

 

  • The button’s id is btn.
  • The button’s initial text label is . After each click, the button must increment by . Recall that the button’s text label is the JS object’s innerHTML property.
  • The button has the following style properties:
  • A width of 96px.
  • A height of 48px.
  • The font-size attribute is 24px.

 

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:

 

1
2
3
4
5
6
7
8
9
10
 







 

Submissions

This is a new style of challenge involving Front-End rendering. It may take up to seconds to see the result of your code, so please be patient after clicking Submit. The Submissions page contains screenshots to help you gauge how well you did.

 

Ask questions in the Discussions forum and submit any bug reports to support@hackerrank.com. Enjoy!

 

Explanation

Initially, the button looks like this:


0

 

 

After the first 4 clicks, it looks like this:


4

 

 

After 5 more clicks, it looks like this:


5

 

 

 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


document.addEventListener('DOMContentLoaded', () => {
const button = document.getElementById('btn');

button.addEventListener('click', (e) => {
        const count = Number(e.currentTarget.innerText) + 1;
        
        e.currentTarget.innerText = count;
        });
});  
Copy The Code & Try With Live Editor

#2 Code Example with HTML

Code - CSS


document.addEventListener('DOMContentLoaded', () => {
const button = document.getElementById('btn');

button.addEventListener('click', (e) => {
        const count = Number(e.currentTarget.innerText) + 1;
        
        e.currentTarget.innerText = count;
        });
});  
Copy The Code & Try With Live Editor

#3 Code Example with CSS

Code - CSS


document.addEventListener('DOMContentLoaded', () => {
const button = document.getElementById('btn');

button.addEventListener('click', (e) => {
        const count = Number(e.currentTarget.innerText) + 1;
        
        e.currentTarget.innerText = count;
        });
});  
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] #6 Day 6: Bitwise Operators in Javascript HackerRank - HackerRank Javascript in 10 days
Next
[Solved] #9 Day 9: Binary Calculator in Javascript HackerRank - HackerRank Javascript in 10 days