Day 9: Binary Calculator in JavaScript | 10 Days Of JavaScript Solutions | HackerRank Programming Solutions [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, 10 Days Of JavaScript. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 9: Binary Calculator in JavaScript-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To JavaScript

JavaScript is a lightweight, cross-platform, and interpreted scripting language. It is well-known for the development of web pages, many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side developments. JavaScript contains a standard library of objects, like Array, Date, and Math, and a core set of language elements like operators, control structures, and statements.

Advantages of JavaScript

The merits of using JavaScript are −

  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.
  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.
  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

Link for the ProblemDay 9: Binary Calculator – Hacker Rank Solution

Day 9: Binary Calculator – Hacker Rank Solution

Problem:

Objective

In this challenge, we implement a calculator that uses binary numbers. Check out the attached tutorial for learning materials.

Task

Implement a simple calculator that performs the following operations on binary numbers: addition, subtraction, multiplication, and division. Note that division operation must be integer division only; for example, , , and .

The calculator’s initial state must look like this:

image
  • Element IDs. Each element in the document must have an id, specified below:innerHTMLidDescription/BehaviorresContains the result of button presses.btnsA button container that displays all eight calculator buttons.0btn0A button expressing binary digit .1btn1A button expressing binary digit .CbtnClrA button to clear the contents of .=btnEqlA button to evaluate the contents of the expression in .+btnSumA button for the addition operation.-btnSubA button for the subtraction operation.*btnMulA button for the multiplication operation./btnDivA button for the integer division operation.
  • Styling. The document’s elements must have the following styles:
    • body has a width of 33%.
    • res has a background-color of lightgray, a border that is solid, a height of 48px, and a font-size of 20px.
    • btn0 and btn1 have a background-color of lightgreen and a color of brown.
    • btnClr and btnEql have a background-color of darkgreen and a color of white.
    • btnSumbtnSubbtnMul, and btnDiv have a background-color of black, a color of red.
    • All the buttons in btns have a width of 25%, a height of 36px, a font-size of 18pxmargin of 0px, and float value left.

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/binaryCalculator.css" type="text/css">
    </head>
    
    <body>
    	<script src="js/binaryCalculator.js" type="text/javascript"></script>
    </body>
</html>

Constraints

  • All expressions in the test dataset are entered in the form , where  is the first binary number,  is the second binary number, and  is in the set .
  • Both operands will always be positive integers when converted from base- to base-.
  • All expressions will be valid.

Explanation

Consider the following sequence of button clicks:

Before pressing the  button, the result div looks like this:

27+8

After pressing the  button to evaluate our expression, the result div looks like this:

27+8eval

Notice that , , and , so our calculator evaluated the expression correctly.

Now, let’s consider our next sequence of button clicks as:

Before pressing the  button, the result div looks like this:

141x7

After pressing the  button to evaluate our expression, the result div looks like this:

141x7eval

Consider the next sequence of button clicks as:

The result div looks like this:

clear3
Day 9: Binary Calculator – Hacker Rank Solution
<!-- HTML -->
<input id="res"></input>
<div id="btns">
    <button id="btn0">0</button>
    <button id="btn1">1</button>
    <button id="btnClr">C</button>
    <button id="btnEql">=</button>
    <button id="btnSum">+</button>
    <button id="btnSub">-</button>
    <button id="btnMul">*</button>
    <button id="btnDiv">/</button>
</div>
/* CSS */
body {
    width: 33%;
}

#res {
    background-color: light-gray;
    border: solid;
    height: 48px;
    font-size: 20px;
}

button {
    width: 25%;
    height: 36px;
    font-size: 18px;
    margin: 0px;
    float: left;
}

#btn0, #btn1 {
    background-color: lightgreen;
    color: brown;
}

#btnClr, #btnEql {
    background-color: darkgreen;
    color: white;
}

#btnSum, #btnSub, #btnMul, #btnDiv {
    background-color: black;
    color: red;
}
// Work in progress! 

// JS
const ids = [1, 2, 3, 6, 9, 8, 7, 4]; // start position ids in clockwise order
let nums = [1, 2, 3, 6, 9, 8, 7, 4]; // rotate in clockwise order
let btn5 = document.getElementById("btn5");

btn5.onclick = function() {
    nums.unshift(nums.pop());
    for (i = 0; i <= 7; i++) {
        document.getElementById("btn" + ids[i]).innerHTML = nums[i];
    }
};

26 thoughts on “Day 9: Binary Calculator in JavaScript | 10 Days Of JavaScript Solutions | HackerRank Programming Solutions [💯Correct]”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock