Created
June 1, 2017 10:15
-
-
Save musale/107258a9fd1e89d30dda1963cfb00c5c to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zenopub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | |
var Stack = (function () { | |
function Stack() { | |
_classCallCheck(this, Stack); | |
this.items = []; | |
} | |
_createClass(Stack, [{ | |
key: 'push', | |
value: function push(element) { | |
this.items.push(element); | |
} | |
}, { | |
key: 'pop', | |
value: function pop() { | |
return this.items.pop(); | |
} | |
}, { | |
key: 'peek', | |
value: function peek() { | |
return this.items[this.items.length - 1]; | |
} | |
}, { | |
key: 'isEmpty', | |
value: function isEmpty() { | |
return this.items.length == 0; | |
} | |
}, { | |
key: 'size', | |
value: function size() { | |
return this.items.length; | |
} | |
}, { | |
key: 'clear', | |
value: function clear() { | |
this.items = []; | |
} | |
}, { | |
key: 'print', | |
value: function print() { | |
console.log(this.toString()); | |
} | |
}, { | |
key: 'toString', | |
value: function toString() { | |
return this.items.toString(); | |
} | |
}]); | |
return Stack; | |
})(); | |
function divideBy2(decNumber) { | |
var remStack = new Stack(), | |
rem, | |
binaryString = ''; | |
while (decNumber > 0) { | |
rem = Math.floor(decNumber % 2); | |
remStack.push(rem); | |
decNumber = Math.floor(decNumber / 2); | |
} | |
while (!remStack.isEmpty()) { | |
binaryString += remStack.pop().toString(); | |
} | |
return binaryString; | |
} | |
console.log(divideBy2(233)); | |
console.log(divideBy2(10)); | |
console.log(divideBy2(1000)); | |
/* | |
The folow algorithm converts from base 10 to any base | |
*/ | |
function baseConverter(decNumber, base) { | |
var remStack = new Stack(), | |
rem, | |
baseString = '', | |
digits = '0123456789ABCDEF'; | |
while (decNumber > 0) { | |
rem = Math.floor(decNumber % base); | |
remStack.push(rem); | |
decNumber = Math.floor(decNumber / base); | |
} | |
while (!remStack.isEmpty()) { | |
baseString += digits[remStack.pop()]; | |
} | |
return baseString; | |
} | |
console.log(baseConverter(100345, 2)); | |
console.log(baseConverter(100345, 8)); | |
console.log(baseConverter(100345, 16)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">class Stack { | |
constructor () { | |
this.items = []; | |
} | |
push(element){ | |
this.items.push(element); | |
} | |
pop(){ | |
return this.items.pop(); | |
} | |
peek(){ | |
return this.items[this.items.length-1]; | |
} | |
isEmpty(){ | |
return this.items.length == 0; | |
} | |
size(){ | |
return this.items.length; | |
} | |
clear(){ | |
this.items = []; | |
} | |
print(){ | |
console.log(this.toString()); | |
} | |
toString(){ | |
return this.items.toString(); | |
} | |
} | |
function divideBy2(decNumber){ | |
var remStack = new Stack(), | |
rem, | |
binaryString = ''; | |
while (decNumber > 0){ | |
rem = Math.floor(decNumber % 2); | |
remStack.push(rem); | |
decNumber = Math.floor(decNumber / 2); | |
} | |
while (!remStack.isEmpty()){ | |
binaryString += remStack.pop().toString(); | |
} | |
return binaryString; | |
} | |
console.log(divideBy2(233)); | |
console.log(divideBy2(10)); | |
console.log(divideBy2(1000)); | |
/* | |
The folow algorithm converts from base 10 to any base | |
*/ | |
function baseConverter(decNumber, base){ | |
var remStack = new Stack(), | |
rem, | |
baseString = '', | |
digits = '0123456789ABCDEF'; | |
while (decNumber > 0){ | |
rem = Math.floor(decNumber % base); | |
remStack.push(rem); | |
decNumber = Math.floor(decNumber / base); | |
} | |
while (!remStack.isEmpty()){ | |
baseString += digits[remStack.pop()]; | |
} | |
return baseString; | |
} | |
console.log(baseConverter(100345, 2)); | |
console.log(baseConverter(100345, 8)); | |
console.log(baseConverter(100345, 16));</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | |
var Stack = (function () { | |
function Stack() { | |
_classCallCheck(this, Stack); | |
this.items = []; | |
} | |
_createClass(Stack, [{ | |
key: 'push', | |
value: function push(element) { | |
this.items.push(element); | |
} | |
}, { | |
key: 'pop', | |
value: function pop() { | |
return this.items.pop(); | |
} | |
}, { | |
key: 'peek', | |
value: function peek() { | |
return this.items[this.items.length - 1]; | |
} | |
}, { | |
key: 'isEmpty', | |
value: function isEmpty() { | |
return this.items.length == 0; | |
} | |
}, { | |
key: 'size', | |
value: function size() { | |
return this.items.length; | |
} | |
}, { | |
key: 'clear', | |
value: function clear() { | |
this.items = []; | |
} | |
}, { | |
key: 'print', | |
value: function print() { | |
console.log(this.toString()); | |
} | |
}, { | |
key: 'toString', | |
value: function toString() { | |
return this.items.toString(); | |
} | |
}]); | |
return Stack; | |
})(); | |
function divideBy2(decNumber) { | |
var remStack = new Stack(), | |
rem, | |
binaryString = ''; | |
while (decNumber > 0) { | |
rem = Math.floor(decNumber % 2); | |
remStack.push(rem); | |
decNumber = Math.floor(decNumber / 2); | |
} | |
while (!remStack.isEmpty()) { | |
binaryString += remStack.pop().toString(); | |
} | |
return binaryString; | |
} | |
console.log(divideBy2(233)); | |
console.log(divideBy2(10)); | |
console.log(divideBy2(1000)); | |
/* | |
The folow algorithm converts from base 10 to any base | |
*/ | |
function baseConverter(decNumber, base) { | |
var remStack = new Stack(), | |
rem, | |
baseString = '', | |
digits = '0123456789ABCDEF'; | |
while (decNumber > 0) { | |
rem = Math.floor(decNumber % base); | |
remStack.push(rem); | |
decNumber = Math.floor(decNumber / base); | |
} | |
while (!remStack.isEmpty()) { | |
baseString += digits[remStack.pop()]; | |
} | |
return baseString; | |
} | |
console.log(baseConverter(100345, 2)); | |
console.log(baseConverter(100345, 8)); | |
console.log(baseConverter(100345, 16)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment