JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<!DOCTYPE html>
<html>
<head>
<title>8bit Calculator</title>
<meta name="description" content="A cool thing made">
<link id="favicon" rel="icon" href="public/cal.ico" type="image/x-icon">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="public/style.css">
</head>
<body>
<main>
<header>
<h1>
Electronic Calculator
</h1>
</header>
<form>
<div class="top">
<div>
<input class="screen" type="text" maxlength="100" placeholder="0">
</div>
<div class="ticker"></div>
</div>
<div class="mid">
<div class="operator">
<!-- operators go here -->
</div>
</div>
</form>
</main>
<!-- Yourdiv web-app is https, so your scripts need to be too -->
<script src="https://code.jquery.com/jquery-2.2.1.min.js"
integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00="
crossorigin="anonymous"></script>
<script src="public/client.js"></script>
</body>
</html>
/* styles */
/* called by your view template */
/* You might want to try something fancier: */
/* less: http://lesscss.org/ */
* {
box-sizing: border-box;
}
body {
font-family: helvetica, arial, sans-serif;
margin: 25px;
}
h1 {
font-weight: bold;
color: pink;
}
.bold {
font-weight: bold;
}
p {
max-width: 600px;
}
form {
margin-bottom: 25px;
padding: 15px;
background-color: cyan;
display: inline-block;
width: 100%;
max-width: 340px;
border-radius: 3px;
}
input {
display: block;
margin-bottom: 10px;
padding: 5px;
width: 100%;
border: 1px solid lightgrey;
border-radius: 3px;
font-size: 16px;
}
button {
font-size: 16px;
border-radius: 3px;
background-color: lightgrey;
border: 1px solid grey;
box-shadow: 2px 2px teal;
padding:10px 15px;
cursor: pointer;
width: 50px;
}
button.double {
padding:10px 30px;
}
button.double.long {
padding: 35px 15px 40px 15px;
}
button.double.lat {
padding-top: 10px;
padding-right: 65px;
padding-bottom: 10px;
padding-left: 65px;
}
.yellow{
background-color: yellow
}
button:hover {
background-color: yellow;
}
button:active {
box-shadow: none;
}
li {
margin-bottom: 5px;
}
footer {
margin-top: 50px;
padding-top: 25px;
border-top: 1px solid lightgrey;
}
footer > a {
color: #BBBBBB;
}
.nicejob {
text-decoration: line-through;
}
.screen{
width:100%;
line-height:40px ;
font-size:2em;
height:40px;
text-align:right;
}
.ticker{
text-align:right;
}
.operator{
font-size:1.5em;
font-weight:bold;
float:left;
}
.operator div {
padding:10px;
min-width: 77.5px;
float:left;
}
main{
width: 340px;
margin: 0 auto;
}
.zero{
margin-top: -55px;
}
.point{
margin-top: -55px;
}
@media only screen and (max-width: 400px) {
button {
font-size: 16px;
border-radius: 3px;
background-color: lightgrey;
border: 1px solid grey;
box-shadow: 2px 2px teal;
padding:7px 7px;
cursor: pointer;
min-width: 40px;
}
.operator div {
padding:10px;
min-width: 77.5px;
float:left;
}
}
// client-side js
// run by the browser each time your view template is loaded
// by default, you've got jQuery,
// add other scripts at the bottom of index.html
$(function() {
// model
var data = {
init: function(){
this.data = [];
this.operations = [];
this.lastEntry;
},
keypad: {
0:{v: 'AC', type: "operators", style: "single", pos: "long", custom: ""},
1:{v: 'CE', type: "operators", style: "single", pos: "long", custom: ""},
2:{v: '/', type: "operators", style: "single", pos: "long", custom: ""},
3:{v: '*', type: "operators", style: "single", pos: "long", custom: ""},
4:{v: 7, type: "digit", style: "single", pos: "long", custom: ""},
5:{v: 8, type: "digit", style: "single", pos: "long", custom: ""},
6:{v: 9, type: "digit", style: "single", pos: "long", custom: ""},
7:{v: '-', type: "operators", style: "single", pos: "long", custom: ""},
8:{v: 4, type: "digit", style: "single", pos: "long", custom: ""},
9:{v: 5, type: "digit", style: "single", pos: "long", custom: ""},
10:{v: 6, type: "digit", style: "single", pos: "long", custom: ""},
11:{v: '+', type: "operators", style: "single", pos: "long", custom: ""},
12:{v: 1, type: "digit", style: "single", pos: "long", custom: ""},
13:{v: 2, type: "digit", style: "single", pos: "long", custom: ""},
14:{v: 3, type: "digit", style: "single", pos: "long", custom: ""},
15:{v: '=', type: "operators", style: "double yellow ", pos: "long", custom: ""},
16:{v: 0, type: "digit", style: "double", pos: "lat", custom: " zero "},
17:{v: '.', type: "digit", style: "single", pos: "long", custom: " point "},
},
getkeypad: function(){
return this.keypad;
}
};
//controller
var octopus = {
init: function() {
data.init();
view.init();
},
tempValue:0,
addEntry: function(v, operator) {
if( v === 'CE'){
(data.operations.pop());
}
else if (v === "=" && data.operations.length > 0) {
(this.sum());
} else {
if (data.operations.length > 0 // array > 0
&&
(Array.isArray(data.operations[(data.operations.length - 1)]) === false)) { //last entry is not array
if (operator) { //if new entry is an operator
data.operations.push([v]);
} else {
data.operations[(data.operations.length - 1)] = data.operations[(data.operations.length - 1)].toString() + v.toString();
}
} else {
if(v === "."){
v = '0' + v.toString();
}
operator && v !== 0 ? data.operations[(data.operations.length - 1)] = ([v]) : data.operations.push(v);
}
}
},
getOperation: function(v) {
if( v === 'AC'){
data.init();
}else if(v === '='){
return this.tempValue;
}else if (data.operations.length > 0) {
if (Array.isArray(data.operations[(data.operations.length - 1)])) {
return data.operations[data.operations.length - 1];
} else {
return data.operations[data.operations.length - 1];
}
} else {
return "";
}
},
getEntries: function() {
var tickerString = data.operations.map(function(v, i) {
v = Array.isArray(v) ? v[0] : v;
return v;
});
return tickerString.join(" ")
},
sum: function(v) {
var expression = data.operations.map(function(v, i) {
v = Array.isArray(v) ? v[0] : v;
return v;
});
console.log(expression)
this.tempValue = eval(expression.join(" "));
}
};
// html view
var view = {
init: function() {
this.displayKeypad();
},
handle: '.operator',
screen: '.screen',
notifier: '.notify',
ticker: '.ticker',
notify: function(msg) {},
displayKeypad: function() {
var keypad = data.getkeypad();
var html = '';
for (var i = 0; i < Object.keys(keypad).length; i++) {
var key = Object.keys(keypad)[i];
html += '<div class="' + keypad[key].custom + '"><button data-v="' + keypad[key].v + '" class="keypad ' + keypad[key].type + ' ' + keypad[key].pos + ' ' + keypad[key].style + '" >' + keypad[key].v + '</button></div>';
};
$(this.handle).append(html);
$(this.handle).find('.keypad').on({
'click': function(e) {
e.preventDefault();
octopus.addEntry($(this).text(), $(this).hasClass('operators'));
// update screen
view.displayScreen(octopus.getOperation($(this).text()));
view.updateTicker();
}
});
},
displayScreen: function(entry) {
$(this.screen).val(entry);
},
updateTicker: function() {
$(this.ticker).text(octopus.getEntries());
}
};
octopus.init();
});
Also see: Tab Triggers