-
Notifications
You must be signed in to change notification settings - Fork 183
/
binaryCalculator.js
55 lines (49 loc) · 1.51 KB
/
binaryCalculator.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
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/js10-binary-calculator/problem
// Difficulty: Medium
// Max Score: 30
// Language: JavaScript (Node.js)
// ========================
// Solution
// ========================
var screen = "";
function operatorFunx() {
if (screen.indexOf("+") != -1) {
var numbers = screen.split("+");
var x = parseInt(numbers[0], 2);
var y = parseInt(numbers[1], 2);
var sum = x + y;
var ans = sum.toString(2);
} else if (screen.indexOf("-") != -1) {
var numbers = screen.split("-");
var x = parseInt(numbers[0], 2);
var y = parseInt(numbers[1], 2);
var sub = x - y;
var ans = sub.toString(2);
} else if (screen.indexOf("*") != -1) {
var numbers = screen.split("*");
var x = parseInt(numbers[0], 2);
var y = parseInt(numbers[1], 2);
var mul = x * y;
var ans = mul.toString(2);
} else if (screen.indexOf("/") != -1) {
var numbers = screen.split("/");
var x = parseInt(numbers[0], 2);
var y = parseInt(numbers[1], 2);
var div = x / y;
var ans = div.toString(2);
}
screen = ans;
document.getElementById("res").innerHTML = screen;
}
function key(c) {
//console.log(screen);
screen += c;
document.getElementById("res").innerHTML = screen;
}
function cl() {
screen = "";
document.getElementById("res").innerHTML = screen;
};