var text = document.getElementById('text')
, button = document.getElementById('button')
, label = document.getElementById('label')
, colors = document.getElementById('colors')
, value = ''
, decimal = document.getElementById('decimal')
, string = document.getElementById('string')
, colorstring = '';
function splitString (string, size) {
var re = new RegExp('.{1,' + size + '}', 'g');
return string.match(re);
}
text.addEventListener('keypress',function(e){if(e.charCode ==13)button.click();});
button.addEventListener('click',function(){
value = '';
colorstring = '';
text.value.split('').forEach(function(e){
switch(e.charCodeAt(0).toString().length){
case 1:value +="00"+e.charCodeAt(0);break;
case 2:value +="0"+e.charCodeAt(0);break;
case 3:value +=e.charCodeAt(0);break;
}
// value += e.charCodeAt(0);
});
value = decToHex(value);
splitString(value,6).forEach(function(e,i){
colorstring +="<div style='background:#"+e+"'>"+e+"</div>";
});
decimal.innerHTML = hexToDec(value);
colors.innerHTML = colorstring;
label.innerHTML = value;
string.innerHTML = splitString( hexToDec(value).split('').reverse().join('') ,3).reverse().map( function(e){ return String.fromCharCode( e.split('').reverse().join('') );} ).join('');
});
function add(x, y, base) {
var z = [];
var n = Math.max(x.length, y.length);
var carry = 0;
var i = 0;
while (i < n || carry) {
var xi = i < x.length ? x[i] : 0;
var yi = i < y.length ? y[i] : 0;
var zi = carry + xi + yi;
z.push(zi % base);
carry = Math.floor(zi / base);
i++;
}
return z;
}
// Returns a*x, where x is an array of decimal digits and a is an ordinary
// JavaScript number. base is the number base of the array x.
function multiplyByNumber(num, x, base) {
if (num < 0) return null;
if (num == 0) return [];
var result = [];
var power = x;
while (true) {
if (num & 1) {
result = add(result,...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.